mushroomdb-server 0.6.9

HTTP/WebSocket server and MCP adapter for mushroomdb
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
//! The fourteen MCP tools that answer a question in prose rather than in JSON.
//!
//! `explore`, `map`, `context`, `impact`, `owners`, `why`, `recall`,
//! `remember` and `sync` sit in front of the thirteen graph tools in
//! `mcp::tools_list`, because they are what an assistant working in a checkout
//! actually reaches for: find me this thing, what is this repository, what is
//! this symbol, what does my diff touch, who wrote this, why are these two
//! linked, what do I already know, remember this, and bring the store up to
//! date. `explain_association` answers on a store with no repository in it:
//! why these two entities are associated, with the rule that derived each
//! edge.
//!
//! Four more answer the rest of the entity graph's questions, and they are the
//! ones the first association benchmark run showed an assistant failing to
//! find. `node_edges` and `neighborhood` used to hand back a JSON array of
//! `{edge_type, src_key, dst_key, derived}` — a listing with no rule, no score
//! and no evidence, which is why a run spent 195 `query` calls and 66
//! `edge_history` calls reconstructing what one reply could have said. Both
//! now answer in prose, grouped by edge type, each listed edge carrying the
//! rule that derived it, its score, and the predicate it matched on.
//! `edges_at` answers the same question at a past commit, and `what_if`
//! answers it about a change that has not been made.
//!
//! `explore` is the composition of `context`, `impact` and `owners` behind one
//! name, and on a store a repository was ingested into it is the *only* task
//! tool `tools/list` advertises — see `mcp::Surface`. The rest stay callable
//! and are one `--all-tools` away.
//!
//! # Shape of a reply
//!
//! Every tool here answers with the rendered digest as its **text content and
//! nothing else**. It used to ship the serialised report alongside it as
//! `structuredContent`, with the same digest repeated under a `text` key: on
//! seven representative calls that was 11.6 KB of digest against 11.9 KB of
//! exact duplicate and 15.5 KB of restatement, 3.42× the text an assistant
//! reads, and it slipped past the renderers' line budgets — a default `impact`
//! capped its text at 25 lines while shipping 13 KB of uncapped report beside
//! it. No task tool declares an `outputSchema`, so nothing bound that payload.
//!
//! A program that wants the numbers asks for them: every tool takes an
//! optional `json` boolean, and with it set the reply is the serialised report
//! as the text content, with no rendered digest.
//!
//! # What each one reads and writes
//!
//! All but two are pure reads of the graph. `remember` writes one `Note`, and
//! `sync` writes nothing itself: it runs this binary again as
//! `<exe> sync <db> --json` and hands back what that reports. The server crate
//! cannot depend on the CLI crate that owns the incremental ingest, and
//! re-implementing it here would give two answers to one question.
//!
//! # Reading the working tree
//!
//! Two tools look outside the graph. `context` quotes source from the
//! repository the `GitSync` marker names, which core-api does for us. `impact`
//! defaults its file list to the current diff, taken from `$CLAUDE_PROJECT_DIR`
//! when the host sets it to a checkout and from that same marker otherwise;
//! with neither, it says to pass files explicitly rather than guessing.
//!
//! # Untrusted content
//!
//! Everything these tools render came out of the graph, and a graph built by
//! `ingest-git` holds whatever contributors wrote: author names, paths, commit
//! subjects, doc comments, and — through `context` — lines of the working tree.
//! [`ok`] therefore stamps every reply with
//! [`repograph::UNTRUSTED_FRAMING`], the same marker
//! `recall_digest` puts on its own digest, so an assistant is told to read the
//! lines under it as data before it reads any of them. The renderers already
//! sanitize each line; the framing is what says whose words they are.

use crate::mcp::CallOutcome;
use core_api::repograph::{
    self, ContextOptions, ImpactOptions, MapOptions, RememberInput, DEFAULT_EXCLUDES,
    MAX_OUTPUT_BYTES, NOTE_KINDS, UNTRUSTED_FRAMING,
};
use core_api::{
    json_to_value, Dir, Explanation, GraphError, NodeInfo, PredicateSummary, SharedDb, Value,
};
use serde_json::{json, Value as Js};
use std::collections::{BTreeMap, BTreeSet};
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::process::Command;

/// The `GitSync` marker `ingest-git` writes, and the prop naming the checkout.
///
/// Its presence is also what tells a code-graph store from a memory one, which
/// is how [`mcp::Surface`](crate::mcp) picks the tools to advertise.
pub(crate) const SYNC_KEY: &str = "__mushroomdb_git_sync__";
const SYNC_REPO_PROP: &str = "repo";

/// The host's project directory: the checkout an assistant is working in.
const PROJECT_DIR_VAR: &str = "CLAUDE_PROJECT_DIR";

/// The fourteen names this module answers to. Listed once, so the `json`
/// argument below is read for exactly the tools that declare it.
///
/// `explore` comes first because it is the whole default surface of a
/// code-graph store: the one tool a session finds, composed from the three
/// beneath it. `explain_association` sits beside `why` because they are the
/// same question asked of the two doors: what links these two, with the
/// evidence — `why` from a code graph, `explain_association` from the rules
/// that derived the edge. The four entity tools follow it, because they are
/// the same question widened: every relationship of one node rather than of
/// one pair, that listing at a past commit, and that listing under a change
/// that has not been made.
pub(crate) const TASK_TOOLS: [&str; 14] = [
    "explore",
    "map",
    "context",
    "impact",
    "owners",
    "why",
    "explain_association",
    "node_edges",
    "neighborhood",
    "edges_at",
    "what_if",
    "recall",
    "remember",
    "sync",
];

/// Route a task tool. `None` when `name` is not one of the fourteen.
pub(crate) fn dispatch(
    db: &SharedDb,
    db_dir: Option<&Path>,
    name: &str,
    args: &Js,
) -> Option<CallOutcome> {
    if !TASK_TOOLS.contains(&name) {
        return None;
    }
    // Every task tool takes the same optional `json`, so it is read and
    // type-checked once here rather than ten times — and before any work, so
    // a caller that mistyped it is told so rather than served a digest it did
    // not ask for.
    let json_out = match bool_arg(args, "json") {
        Ok(b) => b,
        Err(e) => return Some(CallOutcome::ToolErr(e)),
    };
    Some(match name {
        "explore" => tool_explore(db, args, json_out),
        "map" => tool_map(db, json_out),
        "context" => tool_context(db, args, json_out),
        // The one environment read on this path, done here so every function
        // below takes the value and can be tested without touching the
        // process environment.
        "impact" => tool_impact(
            db,
            args,
            std::env::var_os(PROJECT_DIR_VAR).as_deref(),
            json_out,
        ),
        "owners" => tool_owners(db, args, json_out),
        "why" => tool_why(db, args, json_out),
        "explain_association" => tool_explain_association(db, args, json_out),
        "node_edges" => tool_node_edges(db, args, json_out),
        "neighborhood" => tool_neighborhood(db, args, json_out),
        "edges_at" => tool_edges_at(db, args, json_out),
        "what_if" => tool_what_if(db, args, json_out),
        "recall" => tool_recall(db, db_dir, args, json_out),
        "remember" => tool_remember(db, args, json_out),
        "sync" => tool_sync(db_dir, json_out),
        _ => unreachable!("TASK_TOOLS and this match list the same fourteen names"),
    })
}

/// A successful task reply.
///
/// With `json_out` clear — the default — it is the rendered digest under the
/// untrusted-data framing line, and nothing else: no `structuredContent`, no
/// second copy of the same text. `recall_digest` emits the framing itself, so
/// a digest that already carries it is left alone rather than marked twice.
///
/// With `json_out` set it is the serialised report as the text content, for a
/// program that wants the numbers. The report is never rendered in that case,
/// so nothing is computed twice.
///
/// A JSON reply carries **no framing line**: prefixing one would stop the
/// payload being parseable, and the caller that asked for JSON asked for a
/// document to parse rather than prose to read. It is still graph content, so
/// every string in it goes through [`sanitize_json`] first — the escaping
/// `serde_json` does keeps a control character from breaking the *document*,
/// but says nothing about what the reader sees once it has parsed it.
fn ok<T: serde::Serialize>(
    json_out: bool,
    report: &T,
    render: impl FnOnce(&T) -> String,
) -> CallOutcome {
    if json_out {
        return match serde_json::to_value(report) {
            Ok(mut value) => {
                sanitize_json(&mut value);
                CallOutcome::TaskOk {
                    text: value.to_string(),
                }
            }
            Err(e) => CallOutcome::ToolErr(format!("serialise report: {e}")),
        };
    }
    let text = render(report);
    let text = if text.starts_with(UNTRUSTED_FRAMING) {
        text
    } else {
        format!("{UNTRUSTED_FRAMING}{text}")
    };
    CallOutcome::TaskOk { text }
}

/// Replace the control characters in every string of `value` with spaces.
///
/// Graph content reaches a JSON reply in the **values**: paths, author names,
/// commit subjects, note text, quoted source lines. The keys are the report's
/// own field names, fixed in the Rust types the reports serialise from and in
/// the `sync` child's `--json` output, so they carry nothing an outsider wrote
/// and are left alone — rewriting a key could silently merge two of them.
///
/// Newline and tab survive; every other control character does not. That is
/// the one place this differs from [`repograph::sanitize`], and the reason is
/// what the two channels are. A digest is line-structured, so a newline inside
/// a value could forge a heading or an extra hit and has to go. A JSON value is
/// delimited by the grammar, so a newline inside one cannot escape it — and
/// some of these values *are* multi-line documents: `recall`'s report carries
/// the whole rendered digest, and `context` carries quoted source. Flattening
/// those would corrupt the report to defend against nothing. What is still
/// removed is everything that acts on a reader whatever contains it: escape
/// sequences, carriage returns that overwrite a line, backspace, `DEL`.
fn sanitize_json(value: &mut Js) {
    match value {
        Js::String(s) => {
            if s.chars().any(is_forbidden_control) {
                *s = s
                    .chars()
                    .map(|c| if is_forbidden_control(c) { ' ' } else { c })
                    .collect();
            }
        }
        Js::Array(items) => items.iter_mut().for_each(sanitize_json),
        Js::Object(map) => map.values_mut().for_each(sanitize_json),
        _ => {}
    }
}

/// A control character with no business in a JSON value: everything ASCII
/// control except the two that are ordinary text layout.
fn is_forbidden_control(c: char) -> bool {
    c.is_ascii_control() && c != '\n' && c != '\t'
}

/// An optional boolean argument. `Err` when present but wrong-typed.
fn bool_arg(args: &Js, name: &str) -> Result<bool, String> {
    match args.get(name) {
        None | Some(Js::Null) => Ok(false),
        Some(Js::Bool(b)) => Ok(*b),
        Some(_) => Err(format!("{name} must be a boolean")),
    }
}

/// A required string argument.
fn str_arg<'a>(args: &'a Js, name: &str) -> Result<&'a str, String> {
    args.get(name)
        .and_then(Js::as_str)
        .filter(|s| !s.is_empty())
        .ok_or_else(|| format!("missing {name}"))
}

/// An optional string argument. `Err` when present but not a non-empty string.
///
/// An empty string is a filter that matches nothing, which no caller means —
/// they mean "no filter" — so it is refused rather than answered with zero
/// edges.
fn opt_str_arg<'a>(args: &'a Js, name: &str) -> Result<Option<&'a str>, String> {
    match args.get(name) {
        None | Some(Js::Null) => Ok(None),
        Some(Js::String(s)) if !s.is_empty() => Ok(Some(s.as_str())),
        Some(_) => Err(format!("{name} must be a non-empty string")),
    }
}

/// An optional array-of-strings argument. `Err` when present but wrong-typed.
fn str_list_arg(args: &Js, name: &str) -> Result<Vec<String>, String> {
    let Some(v) = args.get(name) else {
        return Ok(Vec::new());
    };
    if v.is_null() {
        return Ok(Vec::new());
    }
    let arr = v
        .as_array()
        .ok_or_else(|| format!("{name} must be an array of strings"))?;
    arr.iter()
        .map(|x| {
            x.as_str()
                .map(str::to_string)
                .ok_or_else(|| format!("{name} must be an array of strings"))
        })
        .collect()
}

// ── explore ──────────────────────────────────────────────────────────────────

/// Bytes an assistant's token is taken to be, for turning a `budget` in tokens
/// into one in bytes. Four is the usual English-and-code average, and the
/// budget is a ceiling rather than a measurement, so erring low would only
/// spend less than the caller allowed.
const BYTES_PER_TOKEN: usize = 4;
/// The default `budget`, in tokens: `DEFAULT_EXPLORE_BYTES` back in the unit a
/// caller thinks in, so the two cannot drift.
const DEFAULT_EXPLORE_TOKENS: u64 = (repograph::DEFAULT_EXPLORE_BYTES / BYTES_PER_TOKEN) as u64;
/// The smallest `budget` worth serving, matching the schema's `minimum`. Below
/// this a reply is a header and nothing else, so a smaller number is taken as
/// this one rather than as a request for silence.
const MIN_EXPLORE_TOKENS: u64 = 200;

fn tool_explore(db: &SharedDb, args: &Js, json_out: bool) -> CallOutcome {
    let target = match str_arg(args, "target") {
        Ok(t) => t,
        Err(e) => return CallOutcome::ToolErr(e),
    };
    let depth = match args.get("depth") {
        None | Some(Js::Null) => repograph::Depth::Context,
        Some(Js::String(s)) => match repograph::Depth::parse(s) {
            Some(d) => d,
            None => {
                return CallOutcome::ToolErr(format!(
                    "depth must be one of {}, got {s:?}",
                    repograph::Depth::NAMES.join(", ")
                ))
            }
        },
        Some(_) => return CallOutcome::ToolErr("depth must be a string".into()),
    };
    let tokens = match args.get("budget") {
        None | Some(Js::Null) => DEFAULT_EXPLORE_TOKENS,
        Some(v) => match v.as_u64() {
            Some(n) => n.max(MIN_EXPLORE_TOKENS),
            None => return CallOutcome::ToolErr("budget must be a positive integer".into()),
        },
    };
    let full = match bool_arg(args, "full") {
        Ok(b) => b,
        Err(e) => return CallOutcome::ToolErr(e),
    };
    let budget_bytes = usize::try_from(tokens)
        .unwrap_or(usize::MAX)
        .saturating_mul(BYTES_PER_TOKEN);
    // `None` for the repository, as `context` does: core-api falls back to the
    // `GitSync` marker, which is the checkout the store was built from.
    let report = {
        let g = db.read();
        repograph::explore(&*g, None, target, depth, full)
    };
    ok(json_out, &report, |r| {
        repograph::render_explore(r, budget_bytes)
    })
}

// ── map ──────────────────────────────────────────────────────────────────────

fn tool_map(db: &SharedDb, json_out: bool) -> CallOutcome {
    let map = {
        let g = db.read();
        repograph::repo_map(&*g, &MapOptions::default())
    };
    ok(json_out, &map, repograph::render_map)
}

// ── context ──────────────────────────────────────────────────────────────────

fn tool_context(db: &SharedDb, args: &Js, json_out: bool) -> CallOutcome {
    let target = match str_arg(args, "target") {
        Ok(t) => t,
        Err(e) => return CallOutcome::ToolErr(e),
    };
    let full = match bool_arg(args, "full") {
        Ok(b) => b,
        Err(e) => return CallOutcome::ToolErr(e),
    };
    // `None` for the repository: core-api falls back to the `GitSync` marker,
    // which is the checkout the store was built from.
    let report = {
        let g = db.read();
        repograph::context_with(&*g, None, target, &ContextOptions { source: full })
    };
    ok(json_out, &report, repograph::render_context)
}

// ── impact ───────────────────────────────────────────────────────────────────

/// `project_dir` is the value of `$CLAUDE_PROJECT_DIR`, passed in rather than
/// read here so a test can exercise both branches of [`project_repo`] without
/// mutating the process environment.
fn tool_impact(
    db: &SharedDb,
    args: &Js,
    project_dir: Option<&OsStr>,
    json_out: bool,
) -> CallOutcome {
    let mut files = match str_list_arg(args, "files") {
        Ok(f) => f,
        Err(e) => return CallOutcome::ToolErr(e),
    };
    if files.is_empty() {
        let repo = match project_repo(db, project_dir) {
            Some(r) => r,
            None => {
                return CallOutcome::ToolErr(
                    "no repository to read a diff from: pass files explicitly".into(),
                )
            }
        };
        match changed_paths(&repo) {
            Ok(paths) => files = paths,
            Err(e) => {
                return CallOutcome::ToolErr(format!(
                    "could not read the diff in {}: {e}; pass files explicitly",
                    repo.display()
                ))
            }
        }
    }
    // The caller's whole change is also what decides the `modified` flag: a
    // partner that is itself being edited is a different fact from one that is
    // not, and only this set can tell them apart.
    let modified: BTreeSet<String> = files.iter().cloned().collect();
    let report = {
        let g = db.read();
        repograph::impact(&*g, &files, &modified, &ImpactOptions::default())
    };
    ok(json_out, &report, repograph::render_impact)
}

/// The checkout root a default `impact` reads its diff from: the host's
/// project directory when it named one inside a repository, else the
/// repository the store was built from.
///
/// `$CLAUDE_PROJECT_DIR` wins because an assistant asking "what does my change
/// touch" means the tree it is editing, which is where the host put it. It has
/// to be inside a checkout to win, though: a host that points it at a plain
/// directory has said nothing about the repository the store knows, so the
/// marker still answers rather than the call failing.
///
/// Both branches resolve to the repository **root**, not to the directory that
/// named it, so the two listings in [`changed_paths`] agree about what their
/// paths are relative to — and so those paths match `File` keys, which are
/// root-relative.
fn project_repo(db: &SharedDb, project_dir: Option<&OsStr>) -> Option<PathBuf> {
    if let Some(root) = project_dir.map(Path::new).and_then(repo_root) {
        return Some(root);
    }
    let repo = {
        let g = db.read();
        g.node_ref(SYNC_KEY)
            .and_then(|n| n.prop(SYNC_REPO_PROP))
            .and_then(|v| match v {
                core_api::Value::Str(s) => Some(s),
                _ => None,
            })
    }?;
    repo_root(Path::new(&repo))
}

/// The root of the checkout `dir` is in, or `None` when it is not in one.
fn repo_root(dir: &Path) -> Option<PathBuf> {
    if !dir.is_dir() {
        return None;
    }
    let output = Command::new("git")
        .arg("-C")
        .arg(dir)
        .args(["rev-parse", "--show-toplevel"])
        .output()
        .ok()?;
    if !output.status.success() {
        return None;
    }
    let root = String::from_utf8_lossy(&output.stdout).trim().to_string();
    (!root.is_empty()).then(|| PathBuf::from(root))
}

/// Paths under the checkout rooted at `root` that differ from `HEAD` or are not
/// tracked at all: root-relative, sorted, deduplicated, and filtered by the
/// same [`DEFAULT_EXCLUDES`] the ingest applied.
///
/// The exclusion matters because a path the ingest skipped is a path no `File`
/// node exists for, and reporting it back as `unknown:` reads like a hole in
/// the graph rather than a build artefact the store never wanted.
///
/// `-z` rather than the default listing: git escapes and quotes a path holding
/// a tab, a newline or a non-ASCII byte, and a quoted path matches no key.
/// `root` rather than the directory the caller named: `ls-files` lists relative
/// to the working directory while `diff` lists relative to the root, so running
/// both anywhere but the root would mix two conventions in one list.
fn changed_paths(root: &Path) -> Result<Vec<String>, String> {
    const LISTS: [&[&str]; 2] = [
        &["diff", "--name-only", "-z", "HEAD"],
        &["ls-files", "--others", "--exclude-standard", "-z"],
    ];
    let excludes: Vec<String> = DEFAULT_EXCLUDES.iter().map(|p| (*p).to_string()).collect();
    let mut out: BTreeSet<String> = BTreeSet::new();
    let mut ran = false;
    for args in LISTS {
        let output = Command::new("git")
            .arg("-C")
            .arg(root)
            .args(args)
            .output()
            .map_err(|e| e.to_string())?;
        // `diff HEAD` fails in a repository with no commits yet. Nothing is
        // dirty relative to a head that does not exist, so that is not an error
        // — but if *neither* listing runs, this is not a repository at all.
        if !output.status.success() {
            continue;
        }
        ran = true;
        for path in String::from_utf8_lossy(&output.stdout).split('\0') {
            if !path.is_empty() && !repograph::path_excluded(path, &excludes) {
                out.insert(path.to_string());
            }
        }
    }
    if !ran {
        return Err("git listed nothing there".into());
    }
    Ok(out.into_iter().collect())
}

// ── owners ───────────────────────────────────────────────────────────────────

fn tool_owners(db: &SharedDb, args: &Js, json_out: bool) -> CallOutcome {
    let path = match str_arg(args, "path") {
        Ok(p) => p,
        Err(e) => return CallOutcome::ToolErr(e),
    };
    let report = {
        let g = db.read();
        repograph::owners(&*g, path, None)
    };
    let Some(report) = report else {
        return CallOutcome::ToolErr(format!("no file in the store at {path}"));
    };
    ok(json_out, &report, repograph::render_owners)
}

// ── why ──────────────────────────────────────────────────────────────────────

fn tool_why(db: &SharedDb, args: &Js, json_out: bool) -> CallOutcome {
    let a = match str_arg(args, "a") {
        Ok(v) => v.to_string(),
        Err(e) => return CallOutcome::ToolErr(e),
    };
    let b = match str_arg(args, "b") {
        Ok(v) => v.to_string(),
        Err(e) => return CallOutcome::ToolErr(e),
    };
    // Keys the graph does not hold are an answer, not a failure: the report
    // names them and the digest says `unknown:`, which tells the caller which
    // of the two to fix.
    let report = {
        let g = db.read();
        repograph::why(&*g, &a, &b)
    };
    ok(json_out, &report, repograph::render_why)
}

// ── explain_association ──────────────────────────────────────────────────────

/// Why two entities are associated: every rule-derived edge between them, with
/// the rule that wrote it and the predicate it matched on.
///
/// The report is the same `Vec<Explanation>` the `explain` graph tool has
/// always returned — `json: true` hands it back unchanged. What is new is the
/// default: on an entity store this is the question the door exists for, and
/// an assistant asking it was getting a JSON array to parse where every other
/// question here answers in prose. `explain` is left as it was, for the caller
/// that wants the array without asking.
fn tool_explain_association(db: &SharedDb, args: &Js, json_out: bool) -> CallOutcome {
    let a = match str_arg(args, "a") {
        Ok(v) => v.to_string(),
        Err(e) => return CallOutcome::ToolErr(e),
    };
    let b = match str_arg(args, "b") {
        Ok(v) => v.to_string(),
        Err(e) => return CallOutcome::ToolErr(e),
    };
    // Unlike `why`, a key the graph does not hold is an error here rather than
    // an `unknown:` line: `explain` resolves both keys to dense ids before it
    // looks at a single edge, and that is the engine's answer to give.
    let report = {
        let g = db.read();
        let found = match g.explain(&a, &b) {
            Ok(v) => v,
            Err(e) => return CallOutcome::ToolErr(crate::mcp::graph_err_msg(e)),
        };
        // The matched values are read here, under the same read guard the
        // edges came from, so the evidence cannot describe a graph that has
        // since moved.
        found
            .into_iter()
            .map(|e| {
                // A via-hop rule evaluates its predicate between the *via*
                // node and the destination, not between the two keys the
                // caller asked about, so there is no pair of nodes here whose
                // values would be the evidence — the line still names the hop.
                let evidence = if e.via_edge.is_some() {
                    None
                } else {
                    match (g.node_info(&e.src_key), g.node_info(&e.dst_key)) {
                        (Some(src), Some(dst)) => {
                            predicate_evidence(&e.predicate, &src, &dst, e.weight)
                        }
                        _ => None,
                    }
                };
                ExplainedEdge { edge: e, evidence }
            })
            .collect::<Vec<_>>()
    };
    ok(json_out, &report, |found| {
        render_explanations(&a, &b, found)
    })
}

/// One explained edge, with the values that made the predicate true.
///
/// The `Explanation` fields are flattened, so `json: true` hands back the
/// array it always did with one `evidence` object added per relationship.
#[derive(serde::Serialize)]
struct ExplainedEdge {
    #[serde(flatten)]
    edge: Explanation,
    #[serde(skip_serializing_if = "Option::is_none")]
    evidence: Option<Evidence>,
}

/// What the two nodes actually had in common, per predicate kind.
///
/// Naming the rule and the threshold was never the answer to "why are these
/// two related" — the shared values are. Without them an assistant fetches
/// both nodes' raw property lists and reads them out, which names every
/// value either node holds rather than the ones they share.
#[derive(serde::Serialize)]
#[serde(untagged)]
enum Evidence {
    /// `overlap` — the intersection of the two lists, sorted.
    Shared { field: String, shared: Vec<Js> },
    /// `field_equal` / `key_match` — the one value both carry.
    Value { field: String, value: Js },
    /// `geo_radius` — both points and the distance between them.
    Geo {
        field: String,
        a: Js,
        b: Js,
        km: f64,
    },
    /// `numeric_within` / `vector_similar` — the two sides. For
    /// `vector_similar` the vectors themselves are useless to read, so `a`
    /// and `b` are omitted and only the score stands.
    Pair {
        field: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        a: Option<Js>,
        #[serde(skip_serializing_if = "Option::is_none")]
        b: Option<Js>,
        #[serde(skip_serializing_if = "Option::is_none")]
        similarity: Option<f64>,
    },
    /// `all` / `any` — one entry per branch that contributed.
    Parts { parts: Vec<Evidence> },
}

/// Mean Earth radius, as the rules engine uses for `geo_radius`.
const EARTH_RADIUS_KM: f64 = 6371.0088;

/// Evidence for one predicate, recursing through `all` / `any`.
///
/// `score` is the edge's weight and is passed only at the top level: `all`
/// takes the minimum of its branches and `any` the maximum, so a branch's own
/// score is not recoverable from the edge and a nested `vector_similar` has
/// no similarity to report.
///
/// # Only what matched
///
/// A branch reports evidence **only when that branch is itself satisfied**,
/// thresholds applied: an `overlap` under its `min`, a `numeric_within` past
/// its `tolerance`, a `geo_radius` past its `km`, a `key_match` whose field
/// does not name the other node. Under `any` that is the whole point — one
/// branch carries the edge and the others did not — and printing an unmatched
/// branch stated a reason the engine had rejected (`size_bucket: 1 vs 9` on a
/// `±2` tolerance). Under `all` every branch matched by construction, so the
/// checks change nothing there.
fn predicate_evidence(
    p: &PredicateSummary,
    src: &NodeInfo,
    dst: &NodeInfo,
    score: Option<f64>,
) -> Option<Evidence> {
    if let Some(parts) = &p.parts {
        let parts: Vec<Evidence> = parts
            .iter()
            .filter_map(|q| predicate_evidence(q, src, dst, None))
            .collect();
        return (!parts.is_empty()).then_some(Evidence::Parts { parts });
    }
    let field = p.fields.first()?.clone();
    match p.kind.as_str() {
        "overlap" => {
            let (Some(Value::List(a)), Some(Value::List(b))) =
                (src.props.get(&field), dst.props.get(&field))
            else {
                return None;
            };
            // Compared as the rules engine compares them: only a scalar
            // element is a token, and its type is part of its identity, so a
            // `1` and a `1.0` in two lists are not an overlap.
            let left: BTreeSet<(u8, String)> = a.iter().filter_map(scalar_token).collect();
            let right: BTreeSet<(u8, String)> = b.iter().filter_map(scalar_token).collect();
            let union = left.union(&right).count();
            let mut shared: Vec<String> = left
                .intersection(&right)
                .map(|(_, text)| text.clone())
                .collect();
            shared.sort();
            shared.dedup();
            if shared.is_empty() || union == 0 {
                return None;
            }
            // The rule's own test: the Jaccard ratio, against the `min` the
            // predicate declares. Inside an `any`, a list that overlaps but
            // not enough is a branch the engine rejected.
            let jaccard = shared.len() as f64 / union as f64;
            if p.min.is_some_and(|min| jaccard < min) {
                return None;
            }
            Some(Evidence::Shared {
                field,
                shared: shared.into_iter().map(Js::String).collect(),
            })
        }
        "field_equal" => {
            let v = src.props.get(&field)?;
            (dst.props.get(&field) == Some(v)).then(|| Evidence::Value {
                field,
                value: crate::json::value_to_json(v),
            })
        }
        // A key-match rule reads a foreign key off the source; the value they
        // share is the destination's own key — when the field really does name
        // it, directly or as one element of a list of foreign keys.
        "key_match" => {
            let names_dst = match src.props.get(&field)? {
                Value::Str(s) => s == &dst.key,
                Value::List(items) => items
                    .iter()
                    .any(|v| matches!(v, Value::Str(s) if s == &dst.key)),
                _ => false,
            };
            names_dst.then(|| Evidence::Value {
                field,
                value: Js::String(dst.key.clone()),
            })
        }
        "numeric_within" => {
            let (a, b) = (src.props.get(&field)?, dst.props.get(&field)?);
            let (x, y) = (numeric(a)?, numeric(b)?);
            let delta = (x - y).abs();
            // The rule's own test. A zero tolerance asks for equality.
            let within = match p.tolerance {
                Some(0.0) => delta == 0.0,
                Some(t) => delta <= t,
                None => true,
            };
            within.then(|| Evidence::Pair {
                field,
                a: Some(crate::json::value_to_json(a)),
                b: Some(crate::json::value_to_json(b)),
                similarity: None,
            })
        }
        "geo_radius" => {
            let (alat, alon) = lat_lon(src.props.get(&field)?)?;
            let (blat, blon) = lat_lon(dst.props.get(&field)?)?;
            let km = haversine_km(alat, alon, blat, blon);
            if p.km.is_some_and(|radius| km > radius) {
                return None;
            }
            Some(Evidence::Geo {
                field,
                a: Js::String(format_lat_lon(alat, alon)),
                b: Js::String(format_lat_lon(blat, blon)),
                km: round2(km),
            })
        }
        // The two vectors say nothing a reader can use; the cosine the rule
        // scored does, and that is the edge's weight.
        "vector_similar" => score.map(|sim| Evidence::Pair {
            field,
            a: None,
            b: None,
            similarity: Some(sim),
        }),
        _ => None,
    }
}

/// The comparable token of one list element, as `(type tag, text)`.
///
/// Mirrors `ValueKey::from_value`: a nested list or map is not a token and
/// cannot overlap, and two tokens of different types never match however
/// alike they read.
fn scalar_token(v: &Value) -> Option<(u8, String)> {
    match v {
        Value::Str(s) => Some((0, s.clone())),
        Value::Int(i) => Some((1, i.to_string())),
        Value::Float(f) => Some((2, format!("{f}"))),
        Value::Bool(b) => Some((3, b.to_string())),
        Value::List(_) | Value::Map(_) => None,
    }
}

/// A `[lat, lon]` pair, as `geo_radius` reads it.
fn lat_lon(v: &Value) -> Option<(f64, f64)> {
    let Value::List(items) = v else {
        return None;
    };
    if items.len() != 2 {
        return None;
    }
    Some((numeric(&items[0])?, numeric(&items[1])?))
}

/// A finite number, as the rules engine reads one: an integer or a finite
/// float, and nothing else.
fn numeric(v: &Value) -> Option<f64> {
    match v {
        #[allow(clippy::cast_precision_loss)]
        Value::Int(i) => Some(*i as f64),
        Value::Float(f) if f.is_finite() => Some(*f),
        _ => None,
    }
}

fn format_lat_lon(lat: f64, lon: f64) -> String {
    format!("{:.4},{:.4}", lat, lon)
}

fn round2(km: f64) -> f64 {
    (km * 100.0).round() / 100.0
}

/// Great-circle distance in km — the same formula `geo_radius` scores with,
/// so the printed distance and the edge's score agree.
fn haversine_km(lat1: f64, lon1: f64, lat2: f64, lon2: f64) -> f64 {
    let phi1 = lat1.to_radians();
    let phi2 = lat2.to_radians();
    let dphi = (lat2 - lat1).to_radians();
    let dlam = (lon2 - lon1).to_radians();
    let a = ((dphi / 2.0).sin().powi(2) + phi1.cos() * phi2.cos() * (dlam / 2.0).sin().powi(2))
        .clamp(0.0, 1.0);
    EARTH_RADIUS_KM * 2.0 * a.sqrt().atan2((1.0 - a).sqrt())
}

/// One evidence clause, rendered for the digest line.
fn evidence_summary(e: &Evidence) -> String {
    match e {
        Evidence::Shared { field, shared } => {
            let vals: Vec<String> = shared.iter().map(json_scalar_text).collect();
            format!("{}: {}", repograph::sanitize(field), vals.join(", "))
        }
        Evidence::Value { field, value } => format!(
            "{}: {}",
            repograph::sanitize(field),
            json_scalar_text(value)
        ),
        Evidence::Geo { field, a, b, km } => format!(
            "{}: {} vs {}, {km} km apart",
            repograph::sanitize(field),
            json_scalar_text(a),
            json_scalar_text(b)
        ),
        Evidence::Pair {
            field,
            a: Some(a),
            b: Some(b),
            ..
        } => format!(
            "{}: {} vs {}",
            repograph::sanitize(field),
            json_scalar_text(a),
            json_scalar_text(b)
        ),
        Evidence::Pair {
            field,
            similarity: Some(sim),
            ..
        } => format!("{}: similarity {sim:.2}", repograph::sanitize(field)),
        Evidence::Pair { field, .. } => repograph::sanitize(field),
        Evidence::Parts { parts } => parts
            .iter()
            .map(evidence_summary)
            .collect::<Vec<_>>()
            .join("; "),
    }
}

/// A JSON scalar as the digest prints it: a string without its quotes,
/// anything else as-is. Property values are graph content, so every string
/// goes through [`repograph::sanitize`].
fn json_scalar_text(v: &Js) -> String {
    match v {
        Js::String(s) => repograph::sanitize(s),
        other => other.to_string(),
    }
}

/// One header, then one line per rule-derived edge, capped like every other
/// task digest.
///
/// Rule names, edge types and predicate fields are all graph content — a rule
/// is named by whoever created it — so each goes through
/// [`repograph::sanitize`] before it reaches a line-structured digest.
fn render_explanations(a: &str, b: &str, found: &[ExplainedEdge]) -> String {
    let mut out = format!(
        "mushroomdb explain — {} ↔ {}: {} relationship(s)\n",
        repograph::sanitize(a),
        repograph::sanitize(b),
        found.len()
    );
    if found.is_empty() {
        out.push_str("  none\n");
        return out;
    }
    for ExplainedEdge { edge: e, evidence } in found {
        out.push_str(&format!(
            "  {} via rule {}",
            repograph::sanitize(&e.edge_type),
            repograph::sanitize(&e.rule)
        ));
        if let Some(weight) = e.weight {
            out.push_str(&format!(" (score {weight:.2})"));
        }
        if let Some(via) = &e.via_edge {
            out.push_str(&format!(" via {}", repograph::sanitize(via)));
        }
        out.push_str(&format!(" — {}", predicate_summary(&e.predicate)));
        // The matched values, in brackets, after the threshold that admitted
        // them: "overlap on specialties >= 0.2 [specialties: hospitality,
        // residential]". This is the line that stops an assistant fetching
        // both nodes' raw lists and reading out everything either one holds.
        if let Some(ev) = evidence {
            out.push_str(&format!(" [{}]", evidence_summary(ev)));
        }
        out.push('\n');
    }
    repograph::cap_lines(&out, repograph::MAX_TOOL_LINES)
}

/// A predicate in one clause: what it compares, on which fields, and the
/// threshold it had to clear.
fn predicate_summary(p: &PredicateSummary) -> String {
    let mut out = repograph::sanitize(&p.kind);
    if !p.fields.is_empty() {
        let fields: Vec<String> = p.fields.iter().map(|f| repograph::sanitize(f)).collect();
        out.push_str(&format!(" on {}", fields.join(", ")));
    }
    if let Some(min) = p.min {
        out.push_str(&format!(" >= {min}"));
    }
    if let Some(tolerance) = p.tolerance {
        out.push_str(&format!(" +/- {tolerance}"));
    }
    if let Some(km) = p.km {
        out.push_str(&format!(" within {km} km"));
    }
    if let Some(parts) = &p.parts {
        let inner: Vec<String> = parts.iter().map(predicate_summary).collect();
        out.push_str(&format!(" ({})", inner.join("; ")));
    }
    if p.approximate {
        out.push_str(" (approximate)");
    }
    out
}

// ── node_edges / neighborhood ────────────────────────────────────────────────

/// Edges listed per edge type when the caller names no `limit`, and the
/// number of `explain` calls one type may cost.
const DEFAULT_EDGE_LIMIT: usize = 10;

/// The largest `limit` a caller may ask for.
///
/// The cost of this reply is one `explain` call per listed partner that is
/// joined by a derived edge — about a millisecond each — so the cap on what is
/// listed is also the cap on what the call costs. A hub node has thousands of
/// partners; a reply is a screen, not a dump.
const MAX_EDGE_LIMIT: usize = 100;

/// Longest edge digest, in lines. Wider than [`repograph::MAX_TOOL_LINES`]
/// because this listing is the reply an assistant reads instead of calling
/// `query` twenty times, and a default `limit` over four edge types already
/// runs past twenty-five lines. The header counts every edge whatever is
/// printed, so a capped digest still says how much it is not showing.
const MAX_EDGE_LINES: usize = repograph::MAX_MAP_LINES;

/// Cap a grouped digest at [`MAX_EDGE_LINES`], saying so when it cuts.
///
/// A listing over many edge types runs past the line budget even under a
/// small `limit`, and a budget that cut silently looked exactly like a
/// complete reply — the caller could not tell. The compact forms are the way
/// past it, so the marker names them.
fn cap_grouped(out: &str) -> String {
    if out.lines().count() <= MAX_EDGE_LINES {
        return out.to_string();
    }
    let mut capped = repograph::cap_lines(out, MAX_EDGE_LINES);
    capped.push_str(&format!(
        "… listing capped at {MAX_EDGE_LINES} lines; pass edge_type or all_of for the whole set\n"
    ));
    capped
}

/// One incident edge, with whatever the rules say about it.
///
/// `rule`, `score` and `predicate` are `Some` only for a derived edge that
/// `explain` accounted for: a manual edge was written by a caller, not
/// matched by a predicate, and has nothing to explain.
struct EdgeLine {
    edge_type: String,
    /// The node at the other end. For a self-loop, the node itself.
    other: String,
    /// `true` when the edge runs out of the node asked about.
    outgoing: bool,
    derived: bool,
    rule: Option<String>,
    score: Option<f64>,
    predicate: Option<String>,
}

/// Every edge of one type incident on the node, and the slice of them listed.
struct EdgeGroup {
    edge_type: String,
    /// How many edges of this type the node has, before the `limit`.
    count: usize,
    listed: Vec<EdgeLine>,
}

/// The edges incident on `key`, grouped by edge type, with each listed edge
/// attributed to the rule that derived it.
///
/// `types` and `dir` are the `neighborhood` filters; `node_edges` passes its
/// single `edge_type` as a one-element list and [`Dir::Both`].
///
/// # What this costs
///
/// One `explain(key, other)` call per **distinct partner** among the listed
/// edges that carries a derived edge, memoised across types so a partner
/// joined by three rules costs one call rather than three. Nothing outside the
/// listed slice is explained, so the bound is `limit` partners per edge type.
///
/// That bound is also the one honest limit on the ordering: a score is only
/// known for an edge that was explained, so a type with more than `limit`
/// edges lists the first `limit` the engine returns and orders **those** by
/// score. Ordering all of them by score would mean explaining all of them,
/// which is the cost this cap exists to refuse.
fn node_edge_groups(
    db: &SharedDb,
    key: &str,
    types: Option<&[String]>,
    dir: Dir,
    limit: usize,
    label: Option<&str>,
) -> Result<(usize, Vec<EdgeGroup>), GraphError> {
    let edges = {
        let g = db.read();
        g.node_edges(key)?
    };
    let mut wanted_label = LabelFilter::new(db, label);

    let mut by_type: BTreeMap<String, Vec<(String, bool, bool)>> = BTreeMap::new();
    let mut total = 0usize;
    for e in edges {
        if let Some(wanted) = types {
            if !wanted.iter().any(|t| t == &e.edge_type) {
                continue;
            }
        }
        let outgoing = e.src_key == key;
        match dir {
            Dir::Out if !outgoing => continue,
            Dir::In if outgoing => continue,
            _ => {}
        }
        let other = if outgoing {
            e.dst_key.clone()
        } else {
            e.src_key.clone()
        };
        if !wanted_label.keeps(&other) {
            continue;
        }
        total += 1;
        by_type
            .entry(e.edge_type.clone())
            .or_default()
            .push((other, outgoing, e.derived));
    }

    // Memoised per partner, not per edge: `explain` answers for every rule
    // edge between the pair at once, whatever its type.
    let mut explained: BTreeMap<String, Vec<Explanation>> = BTreeMap::new();
    let mut groups = Vec::with_capacity(by_type.len());
    for (edge_type, rows) in by_type {
        let count = rows.len();
        let mut listed: Vec<EdgeLine> = Vec::with_capacity(count.min(limit));
        for (other, outgoing, derived) in rows.into_iter().take(limit) {
            let mut line = EdgeLine {
                edge_type: edge_type.clone(),
                other,
                outgoing,
                derived,
                rule: None,
                score: None,
                predicate: None,
            };
            if derived {
                if !explained.contains_key(&line.other) {
                    let found = {
                        let g = db.read();
                        g.explain(key, &line.other).unwrap_or_default()
                    };
                    explained.insert(line.other.clone(), found);
                }
                let found = explained.get(&line.other).map(Vec::as_slice).unwrap_or(&[]);
                if let Some(e) = found.iter().find(|e| {
                    e.edge_type == edge_type
                        && if outgoing {
                            e.src_key == key && e.dst_key == line.other
                        } else {
                            e.src_key == line.other && e.dst_key == key
                        }
                }) {
                    line.rule = Some(e.rule.clone());
                    line.score = e.weight;
                    line.predicate = Some(predicate_summary(&e.predicate));
                }
            }
            listed.push(line);
        }
        // Strongest first. An edge with no score — a manual one, or a rule
        // that declares no `weight_prop` — sorts last rather than pretending
        // to a score of zero, and ties break on the partner key so the reply
        // is byte-stable.
        listed.sort_by(|a, b| {
            let sa = a.score.unwrap_or(f64::NEG_INFINITY);
            let sb = b.score.unwrap_or(f64::NEG_INFINITY);
            sb.partial_cmp(&sa)
                .unwrap_or(std::cmp::Ordering::Equal)
                .then_with(|| a.other.cmp(&b.other))
        });
        groups.push(EdgeGroup {
            edge_type,
            count,
            listed,
        });
    }
    Ok((total, groups))
}

/// One header, then one block per edge type: its name, how many edges the node
/// has of it, and the listed ones with their direction, rule, score and
/// predicate.
///
/// Edge types, partner keys, rule names and predicate fields are all graph
/// content, so every one of them goes through [`repograph::sanitize`] before
/// it reaches a line-structured digest.
fn render_edge_groups(key: &str, total: usize, groups: &[EdgeGroup]) -> String {
    let mut out = format!(
        "mushroomdb edges — {}: {total} edge(s) over {} type(s)\n",
        repograph::sanitize(key),
        groups.len()
    );
    if groups.is_empty() {
        out.push_str("  none\n");
        return out;
    }
    for g in groups {
        out.push_str(&format!(
            "{} ({})\n",
            repograph::sanitize(&g.edge_type),
            g.count
        ));
        for e in &g.listed {
            let arrow = if e.outgoing { "→" } else { "←" };
            out.push_str(&format!("  {arrow} {}", repograph::sanitize(&e.other)));
            if let Some(rule) = &e.rule {
                out.push_str(&format!("  rule {}", repograph::sanitize(rule)));
            }
            if let Some(score) = e.score {
                out.push_str(&format!("  score {score:.2}"));
            }
            if let Some(predicate) = &e.predicate {
                out.push_str(&format!(" — {predicate}"));
            }
            out.push('\n');
        }
        if g.count > g.listed.len() {
            out.push_str(&format!("  … and {} more\n", g.count - g.listed.len()));
        }
    }
    cap_grouped(&out)
}

/// The same grouping as a document, for `json: true`.
///
/// `listed` is the top-level count of edges actually in `types[].edges`, the
/// sum of the per-type `listed`. It is what the tool description and the docs
/// promise — "the report carries `listed` and `total`, so a reply that was cut
/// still says how much there was" — and without it a caller had to add the
/// per-type counts up itself to learn whether the reply was whole.
///
/// `label` is echoed when the reply was narrowed by one, the way every other
/// shape of this reply echoes it: a document that does not say what it was
/// filtered by reads as the unfiltered answer.
fn edge_groups_json(key: &str, total: usize, groups: &[EdgeGroup], label: Option<&str>) -> Js {
    let doc = json!({
        "key": key,
        "total": total,
        "listed": groups.iter().map(|g| g.listed.len()).sum::<usize>(),
        "types": groups.iter().map(|g| json!({
            "edge_type": g.edge_type,
            "count": g.count,
            "listed": g.listed.len(),
            "edges": g.listed.iter().map(edge_line_json).collect::<Vec<_>>(),
        })).collect::<Vec<_>>(),
    });
    with_label(doc, label)
}

fn edge_line_json(e: &EdgeLine) -> Js {
    json!({
        "edge_type": e.edge_type,
        "other": e.other,
        "direction": if e.outgoing { "out" } else { "in" },
        "derived": e.derived,
        "rule": e.rule,
        "score": e.score,
        "predicate": e.predicate,
    })
}

/// The `limit` argument, defaulted and clamped.
///
/// A `limit` past `max` is clamped rather than refused — the schema already
/// names the ceiling, and a caller who asks for more means "all of it" — but
/// a zero is a reply with counts and no rows, which no caller means.
fn limit_arg(args: &Js, default: usize, max: usize) -> Result<usize, String> {
    match args.get("limit") {
        None | Some(Js::Null) => Ok(default),
        Some(v) => match v.as_u64() {
            Some(0) | None => Err("limit must be a positive integer".into()),
            Some(n) => Ok(usize::try_from(n).unwrap_or(max).min(max)),
        },
    }
}

/// The `limit` argument: how many edges of each type to list.
fn edge_limit_arg(args: &Js) -> Result<usize, String> {
    limit_arg(args, DEFAULT_EDGE_LIMIT, MAX_EDGE_LIMIT)
}

// ── keys-only partner views ─────────────────────────────────────────────────

/// Partners listed by a keys-only view when the caller names no `limit`.
///
/// Twenty times the grouped view's default: a key is a few bytes where an
/// attributed edge line is a sentence, and the question these views answer —
/// *which* partners — is not answered by a tenth of the set.
const DEFAULT_PARTNER_LIMIT: usize = 200;

/// The largest `limit` a keys-only view honours. A partner set this wide is
/// still only tens of kilobytes, and it is what the caller asked for by name.
const MAX_PARTNER_LIMIT: usize = 2000;

/// Refusing the one pair of arguments that cannot both be honoured: the
/// answer is either an intersection over several types or a listing of one,
/// and silently letting either win would be a reply about a question the
/// caller did not ask.
const ONE_OF_ALL_OF_OR_EDGE_TYPE: &str = "pass one of all_of or edge_type, not both";

/// Columns a wrapped key list fills before it breaks to the next line.
const KEY_WRAP_COLUMNS: usize = 100;

/// Edge type names an "no such edge type" error lists before it counts the
/// rest off. Twenty is enough to recognise the one that was meant on any
/// schema a person designed, and short enough that the error is still an
/// error rather than a schema dump.
const MAX_KNOWN_EDGE_TYPES: usize = 20;

/// The error a keys-only view answers when the type it was asked about is not
/// in the store at all.
///
/// "0 partners" is the *right* answer for a type that exists and this node has
/// none of, and the wrong one for a typo — and the caller cannot tell the two
/// apart, so a misspelling reads as a fact about the graph. The census is the
/// store's own list of edge types, so the reply both names what went wrong and
/// carries what to ask instead.
///
/// `known` is only walked when the answer came back empty: a type that matched
/// something is a type that exists, and the census costs a pass over the
/// topology.
fn unknown_edge_type(db: &SharedDb, named: &[String]) -> Option<String> {
    let known: Vec<String> = {
        let g = db.read();
        g.edge_type_census()
            .into_iter()
            .map(|c| c.edge_type)
            .collect()
    };
    let missing: Vec<&String> = named.iter().filter(|t| !known.contains(t)).collect();
    if missing.is_empty() {
        return None;
    }
    let listed: Vec<String> = known
        .iter()
        .take(MAX_KNOWN_EDGE_TYPES)
        .map(|t| repograph::sanitize(t))
        .collect();
    let rest = known.len().saturating_sub(listed.len());
    let more = if rest > 0 {
        format!(", … and {rest} more")
    } else {
        String::new()
    };
    let names = missing
        .iter()
        .map(|t| repograph::sanitize(t))
        .collect::<Vec<_>>()
        .join(", ");
    Some(if known.is_empty() {
        format!("no edge type named {names}; this store has no edges yet")
    } else {
        format!(
            "no edge type named {names}; this store has: {}{more}",
            listed.join(", ")
        )
    })
}

/// Keeps only the partners carrying one label.
///
/// The label is resolved per partner key and memoised, so a node joined by
/// four rules is looked up once rather than four times, and a filter with no
/// label answers `true` without touching the store at all. The label is the
/// one the node carries **now**, including when the edges being filtered come
/// from a past commit: a node's label is fixed when it is inserted.
struct LabelFilter<'a> {
    db: &'a SharedDb,
    label: Option<String>,
    seen: BTreeMap<String, bool>,
}

impl<'a> LabelFilter<'a> {
    fn new(db: &'a SharedDb, label: Option<&str>) -> Self {
        LabelFilter {
            db,
            label: label.map(str::to_string),
            seen: BTreeMap::new(),
        }
    }

    fn keeps(&mut self, key: &str) -> bool {
        let Some(label) = &self.label else {
            return true;
        };
        if let Some(hit) = self.seen.get(key) {
            return *hit;
        }
        let ok = {
            let g = self.db.read();
            g.node_ref(key).is_some_and(|n| n.label() == label)
        };
        self.seen.insert(key.to_string(), ok);
        ok
    }

    /// Drop every row whose partner does not carry the label.
    fn retain(&mut self, rows: &mut Vec<PartnerEdge>) {
        if self.label.is_none() {
            return;
        }
        rows.retain(|r| self.keeps(&r.other));
    }
}

/// One incident edge reduced to what a keys-only view needs: who is at the
/// other end, by what type, and in which direction.
struct PartnerEdge {
    other: String,
    edge_type: String,
    outgoing: bool,
}

impl PartnerEdge {
    /// `key` must be the node's canonical key — see [`canonical_self`].
    fn of(src_key: &str, dst_key: &str, edge_type: &str, key: &str) -> Self {
        let outgoing = src_key == key;
        PartnerEdge {
            other: if outgoing {
                dst_key.to_string()
            } else {
                src_key.to_string()
            },
            edge_type: edge_type.to_string(),
            outgoing,
        }
    }

    fn kept(&self, dir: Dir) -> bool {
        match dir {
            Dir::Out => self.outgoing,
            Dir::In => !self.outgoing,
            Dir::Both => true,
        }
    }
}

/// The `direction` argument of a keys-only view: `out`, `in`, or `any`
/// (the default). `both` is accepted as a synonym of `any`, because that is
/// what `neighborhood` calls the same thing.
fn partner_dir_arg(args: &Js) -> Result<Dir, String> {
    match args.get("direction") {
        None | Some(Js::Null) => Ok(Dir::Both),
        Some(v) => match v.as_str() {
            Some(s) if s.eq_ignore_ascii_case("out") => Ok(Dir::Out),
            Some(s) if s.eq_ignore_ascii_case("in") => Ok(Dir::In),
            Some(s) if s.eq_ignore_ascii_case("any") || s.eq_ignore_ascii_case("both") => {
                Ok(Dir::Both)
            }
            Some(other) => Err(format!("unknown direction: {}", repograph::sanitize(other))),
            None => Err("direction must be a string".into()),
        },
    }
}

/// The `all_of` argument: the edge types a partner must carry *every* one of.
fn all_of_arg(args: &Js) -> Result<Vec<String>, String> {
    let types = str_list_arg(args, "all_of")?;
    if args.get("all_of").is_some_and(|v| !v.is_null()) && types.is_empty() {
        return Err("all_of must name at least one edge type".into());
    }
    Ok(types)
}

/// The partners joined to the node by **every** type in `all_of`, sorted.
///
/// The intersection is over partners, not edges: a partner carrying two of
/// three named types is not in the answer, however many edges of those two it
/// has. Direction filters the edges considered, so `direction: "out"` asks
/// which partners the node points at by all of the types.
fn partners_linked_by_all(rows: &[PartnerEdge], all_of: &[String], dir: Dir) -> Vec<String> {
    let mut by_partner: BTreeMap<&str, BTreeSet<&str>> = BTreeMap::new();
    for r in rows {
        if !r.kept(dir) {
            continue;
        }
        by_partner
            .entry(r.other.as_str())
            .or_default()
            .insert(r.edge_type.as_str());
    }
    by_partner
        .into_iter()
        .filter(|(_, types)| all_of.iter().all(|t| types.contains(t.as_str())))
        .map(|(k, _)| k.to_string())
        .collect()
}

/// The distinct partners joined by one edge type, sorted, and how many edges
/// of that type there are — the two are equal unless a pair is joined twice.
fn partners_of_type(rows: &[PartnerEdge], edge_type: &str, dir: Dir) -> (usize, Vec<String>) {
    let mut edges = 0usize;
    let mut partners: BTreeSet<&str> = BTreeSet::new();
    for r in rows {
        if r.edge_type != edge_type || !r.kept(dir) {
            continue;
        }
        edges += 1;
        partners.insert(r.other.as_str());
    }
    (
        edges,
        partners.into_iter().map(str::to_string).collect::<Vec<_>>(),
    )
}

/// Append `keys` as `a, b, c`, continuing `lead` and wrapping at
/// [`KEY_WRAP_COLUMNS`].
///
/// Every key goes through [`repograph::sanitize`] — a key is graph content,
/// and these lines are line-structured digests like any other.
fn push_key_list(out: &mut String, lead: &str, keys: &[String]) {
    let mut line = lead.to_string();
    let mut empty = line.is_empty();
    for (i, k) in keys.iter().enumerate() {
        let k = repograph::sanitize(k);
        let comma = usize::from(i + 1 < keys.len());
        if !empty && line.len() + 1 + k.len() + comma > KEY_WRAP_COLUMNS {
            out.push_str(&line);
            out.push('\n');
            line.clear();
            empty = true;
        }
        if !empty {
            line.push(' ');
        }
        line.push_str(&k);
        if comma == 1 {
            line.push(',');
        }
        empty = false;
    }
    if !line.is_empty() {
        out.push_str(&line);
        out.push('\n');
    }
}

/// `lead: k, k, k` plus the `… and N more` the `limit` cut off.
fn push_partner_block(out: &mut String, lead: &str, partners: &[String], limit: usize) {
    let listed = &partners[..partners.len().min(limit)];
    push_key_list(out, lead, listed);
    if partners.len() > listed.len() {
        out.push_str(&format!("… and {} more\n", partners.len() - listed.len()));
    }
}

/// The keys-only answer to "which partners are linked by all of these types".
///
/// Nothing here is capped by line count: `limit` is the cap, the trailing
/// `… and N more` says what it cut, and a capped digest that swallowed that
/// line would be the one shape a caller could not tell from a complete answer.
fn render_all_of(
    tool: &str,
    key: &str,
    at: Option<u64>,
    all_of: &[String],
    partners: &[String],
    limit: usize,
) -> String {
    let types = all_of
        .iter()
        .map(|t| repograph::sanitize(t))
        .collect::<Vec<_>>()
        .join(", ");
    let when = at.map_or_else(String::new, |a| format!(" as of commit {a}"));
    let mut out = format!(
        "mushroomdb {tool} — {}{when} — partners linked by all of {types}: {}\n",
        repograph::sanitize(key),
        partners.len()
    );
    if partners.is_empty() {
        out.push_str("  none\n");
        return out;
    }
    push_partner_block(&mut out, "", partners, limit);
    out
}

/// The `json: true` shape of a keys-only partner answer.
fn partners_json(
    key: &str,
    at: Option<u64>,
    all_of: &[String],
    label: Option<&str>,
    partners: &[String],
    limit: usize,
) -> Js {
    let mut doc = json!({
        "key": key,
        "all_of": all_of,
        "partners": &partners[..partners.len().min(limit)],
        "listed": partners.len().min(limit),
        "total": partners.len(),
    });
    if let Some(a) = at {
        doc["at"] = json!(a);
    }
    if let Some(l) = label {
        doc["label"] = json!(l);
    }
    doc
}

/// The keys-only answer to "which partners does one edge type join".
///
/// The rule is printed once, in the type's header: every edge of a type comes
/// from the rule that declares that type, so repeating it per line said the
/// same words as many times as there were partners.
fn render_type_partners(
    header: String,
    edge_type: &str,
    edges: usize,
    rule: Option<&str>,
    partners: &[String],
    limit: usize,
) -> String {
    let mut out = header;
    if partners.is_empty() {
        out.push_str("  none\n");
        return out;
    }
    let rule = rule.map_or_else(String::new, |r| {
        format!(", rule {}", repograph::sanitize(r))
    });
    let lead = format!("{} ({edges}{rule}):", repograph::sanitize(edge_type));
    push_partner_block(&mut out, &lead, partners, limit);
    out
}

/// The `json: true` shape of a one-type keys-only answer.
fn type_partners_json(
    key: &str,
    at: Option<u64>,
    edge_type: &str,
    rule: Option<&str>,
    edges: usize,
    partners: &[String],
    limit: usize,
) -> Js {
    let mut doc = json!({
        "key": key,
        "edge_type": edge_type,
        "rule": rule,
        "edges": edges,
        "partners": &partners[..partners.len().min(limit)],
        "listed": partners.len().min(limit),
        "total": partners.len(),
    });
    if let Some(a) = at {
        doc["at"] = json!(a);
    }
    doc
}

/// Record the `label` a reply was narrowed by, when it was narrowed at all.
fn with_label(mut doc: Js, label: Option<&str>) -> Js {
    if let Some(l) = label {
        doc["label"] = json!(l);
    }
    doc
}

fn tool_node_edges(db: &SharedDb, args: &Js, json_out: bool) -> CallOutcome {
    let key = match str_arg(args, "key") {
        Ok(k) => k,
        Err(e) => return CallOutcome::ToolErr(e),
    };
    let edge_type = match opt_str_arg(args, "edge_type") {
        Ok(t) => t,
        Err(e) => return CallOutcome::ToolErr(e),
    };
    let all_of = match all_of_arg(args) {
        Ok(t) => t,
        Err(e) => return CallOutcome::ToolErr(e),
    };
    let dir = match partner_dir_arg(args) {
        Ok(d) => d,
        Err(e) => return CallOutcome::ToolErr(e),
    };
    let label = match opt_str_arg(args, "label") {
        Ok(l) => l,
        Err(e) => return CallOutcome::ToolErr(e),
    };

    if !all_of.is_empty() && edge_type.is_some() {
        return CallOutcome::ToolErr(ONE_OF_ALL_OF_OR_EDGE_TYPE.into());
    }

    if !all_of.is_empty() || edge_type.is_some() {
        let limit = match limit_arg(args, DEFAULT_PARTNER_LIMIT, MAX_PARTNER_LIMIT) {
            Ok(n) => n,
            Err(e) => return CallOutcome::ToolErr(e),
        };
        let edges = {
            let g = db.read();
            match g.node_edges(key) {
                Ok(v) => v,
                Err(e) => return CallOutcome::ToolErr(crate::mcp::graph_err_msg(e)),
            }
        };
        let mut rows: Vec<PartnerEdge> = edges
            .iter()
            .map(|e| PartnerEdge::of(&e.src_key, &e.dst_key, &e.edge_type, key))
            .collect();
        LabelFilter::new(db, label).retain(&mut rows);
        if !all_of.is_empty() {
            let partners = partners_linked_by_all(&rows, &all_of, dir);
            // An empty answer is where a typo and a fact look the same; only
            // there is the census worth a pass.
            if partners.is_empty() {
                if let Some(e) = unknown_edge_type(db, &all_of) {
                    return CallOutcome::ToolErr(e);
                }
            }
            let report = partners_json(key, None, &all_of, label, &partners, limit);
            return ok(json_out, &report, |_| {
                render_all_of("edges", key, None, &all_of, &partners, limit)
            });
        }
        let edge_type = edge_type.unwrap_or_default();
        let (count, partners) = partners_of_type(&rows, edge_type, dir);
        if count == 0 {
            if let Some(e) = unknown_edge_type(db, &[edge_type.to_string()]) {
                return CallOutcome::ToolErr(e);
            }
        }
        let rule = live_rule_for_type(db, key, edge_type, partners.first());
        let report = with_label(
            type_partners_json(
                key,
                None,
                edge_type,
                rule.as_deref(),
                count,
                &partners,
                limit,
            ),
            label,
        );
        return ok(json_out, &report, |_| {
            let header = format!(
                "mushroomdb edges — {}: {count} edge(s) over {} type(s)\n",
                repograph::sanitize(key),
                usize::from(count > 0)
            );
            render_type_partners(header, edge_type, count, rule.as_deref(), &partners, limit)
        });
    }

    let limit = match edge_limit_arg(args) {
        Ok(n) => n,
        Err(e) => return CallOutcome::ToolErr(e),
    };
    edge_reply(db, key, None, dir, limit, label, json_out)
}

/// The rule behind one edge type on a live node, from a single `explain` call
/// on one partner — every edge of a type is written by the one rule that
/// declares it, so one pair answers for the whole type. `None` for a manual
/// edge, which no rule derived and which has nothing to name.
fn live_rule_for_type(
    db: &SharedDb,
    key: &str,
    edge_type: &str,
    partner: Option<&String>,
) -> Option<String> {
    let partner = partner?;
    let g = db.read();
    g.explain(key, partner)
        .unwrap_or_default()
        .into_iter()
        .find(|e| e.edge_type == edge_type)
        .map(|e| e.rule)
}

/// Group, render and answer — the tail both `node_edges` and a depth-1
/// `neighborhood` share.
fn edge_reply(
    db: &SharedDb,
    key: &str,
    types: Option<&[String]>,
    dir: Dir,
    limit: usize,
    label: Option<&str>,
    json_out: bool,
) -> CallOutcome {
    match node_edge_groups(db, key, types, dir, limit, label) {
        Ok((total, groups)) => ok(
            json_out,
            &edge_groups_json(key, total, &groups, label),
            |_| render_edge_groups(key, total, &groups),
        ),
        Err(e) => CallOutcome::ToolErr(crate::mcp::graph_err_msg(e)),
    }
}

/// One hop is the edge listing; further than that is still the BFS table.
///
/// Depth 1 is the question this tool is nearly always asked — what is this
/// node joined to — and a table of `(key, label, depth)` answers it without
/// saying *why* any row is there. Past one hop there is no single rule behind
/// a row, so the table is still the honest shape and is returned unchanged.
fn tool_neighborhood(db: &SharedDb, args: &Js, json_out: bool) -> CallOutcome {
    let key = match str_arg(args, "key") {
        Ok(k) => k,
        Err(e) => return CallOutcome::ToolErr(e),
    };
    let depth = match args.get("depth") {
        None | Some(Js::Null) => 1u32,
        Some(v) => match v.as_u64().and_then(|n| u32::try_from(n).ok()) {
            Some(d) => d,
            None => return CallOutcome::ToolErr("depth must be an integer".into()),
        },
    };
    let dir = match args.get("direction") {
        None | Some(Js::Null) => Dir::Both,
        Some(v) => match v.as_str() {
            Some(s) if s.eq_ignore_ascii_case("out") => Dir::Out,
            Some(s) if s.eq_ignore_ascii_case("in") => Dir::In,
            Some(s) if s.eq_ignore_ascii_case("both") => Dir::Both,
            Some(other) => return CallOutcome::ToolErr(format!("unknown direction: {other}")),
            None => return CallOutcome::ToolErr("direction must be a string".into()),
        },
    };
    let edge_types = match str_list_arg(args, "edge_types") {
        Ok(t) => t,
        Err(e) => return CallOutcome::ToolErr(e),
    };
    let label = match opt_str_arg(args, "label") {
        Ok(l) => l,
        Err(e) => return CallOutcome::ToolErr(e),
    };
    let filter = (!edge_types.is_empty()).then_some(edge_types.as_slice());

    if depth <= 1 {
        let limit = match edge_limit_arg(args) {
            Ok(n) => n,
            Err(e) => return CallOutcome::ToolErr(e),
        };
        return edge_reply(db, key, filter, dir, limit, label, json_out);
    }

    let etype_refs: Option<Vec<&str>> = filter.map(|v| v.iter().map(String::as_str).collect());
    let rs = {
        let g = db.read();
        match g.node_ref(key) {
            Some(n) => Ok(n.neighborhood(depth, etype_refs.as_deref(), dir)),
            None => Err(GraphError::KeyNotFound {
                key: key.to_string(),
            }),
        }
    };
    match rs {
        Ok(rs) => CallOutcome::ToolOk(crate::json::result_set_json(&keeping_label(rs, label))),
        Err(e) => CallOutcome::ToolErr(crate::mcp::graph_err_msg(e)),
    }
}

/// A traversal table narrowed to the rows carrying `label`.
///
/// Past one hop the reply is the BFS table, and the table already carries a
/// `label` column — so narrowing it is a filter on rows rather than on the
/// walk. Deliberately not a filter on the *traversal*: a hop through a node of
/// another label is how a two-hop question reaches the label it asked about,
/// and refusing to walk through it would answer a different question. So the
/// walk is whole and the answer is the nodes of that label it reached.
fn keeping_label(rs: core_api::ResultSet, label: Option<&str>) -> core_api::ResultSet {
    let Some(label) = label else {
        return rs;
    };
    let mut out = core_api::ResultSet::new(rs.columns().to_vec());
    for i in 0..rs.len() {
        if rs.get(i, "label") == Some(&Value::Str(label.to_string())) {
            out.push_row(rs.row(i).to_vec());
        }
    }
    out
}

// ── edges_at / what_if shared rendering ─────────────────────────────────────

/// One edge in an `edges_at` or `what_if` reply, as a document: the same
/// shape whichever tool built it, so a caller reading `json: true` sees one
/// edge schema across both.
fn edge_at_json(e: &core_api::EdgeAt) -> Js {
    json!({
        "edge_type": e.edge_type,
        "src": e.src_key,
        "dst": e.dst_key,
        "derived": e.derived,
        "rule": e.rule,
    })
}

// ── edges_at ─────────────────────────────────────────────────────────────────

/// One edge in an `edges_at` text reply, direction already resolved relative
/// to the node the call was about.
struct EdgeAtLine {
    other: String,
    outgoing: bool,
    rule: Option<String>,
}

/// Every edge of one type at the queried commit, and the slice of them listed.
struct EdgeAtGroup {
    edge_type: String,
    count: usize,
    listed: Vec<EdgeAtLine>,
}

/// Group `edges` — every one of them already incident to `key`, sorted by
/// `(edge_type, src_key, dst_key)` by the engine — by edge type, keeping at
/// most `limit` per type for the digest.
fn edges_at_groups(
    edges: Vec<core_api::EdgeAt>,
    key: &str,
    limit: usize,
) -> (usize, Vec<EdgeAtGroup>) {
    let mut by_type: BTreeMap<String, Vec<core_api::EdgeAt>> = BTreeMap::new();
    let mut total = 0usize;
    for e in edges {
        total += 1;
        by_type.entry(e.edge_type.clone()).or_default().push(e);
    }
    let mut groups = Vec::with_capacity(by_type.len());
    for (edge_type, rows) in by_type {
        let count = rows.len();
        let listed = rows
            .into_iter()
            .take(limit)
            .map(|e| {
                let outgoing = e.src_key == key;
                let other = if outgoing { e.dst_key } else { e.src_key };
                EdgeAtLine {
                    other,
                    outgoing,
                    rule: e.rule,
                }
            })
            .collect();
        groups.push(EdgeAtGroup {
            edge_type,
            count,
            listed,
        });
    }
    (total, groups)
}

fn render_edges_at(key: &str, at: u64, total: usize, groups: &[EdgeAtGroup]) -> String {
    let mut out = format!(
        "mushroomdb edges_at — {} as of commit {at}: {total} edge(s)\n",
        repograph::sanitize(key)
    );
    if groups.is_empty() {
        out.push_str("  none\n");
        return out;
    }
    for g in groups {
        out.push_str(&format!(
            "{} ({})\n",
            repograph::sanitize(&g.edge_type),
            g.count
        ));
        for e in &g.listed {
            let arrow = if e.outgoing { "→" } else { "←" };
            out.push_str(&format!("  {arrow} {}", repograph::sanitize(&e.other)));
            if let Some(rule) = &e.rule {
                out.push_str(&format!("  rule {}", repograph::sanitize(rule)));
            }
            out.push('\n');
        }
        if g.count > g.listed.len() {
            out.push_str(&format!("  … and {} more\n", g.count - g.listed.len()));
        }
    }
    cap_grouped(&out)
}

/// The node's canonical current key, recovered from `edges` — every one of
/// them already reported by [`GraphDb::edges_at`] under the name the node
/// carries today, even when the caller queried by an old alias of a renamed
/// node (see `edges_at_reports_a_renamed_nodes_edges_under_the_current_key`
/// in `crates/core-api/tests/edges_at.rs`). A stale `key` therefore never
/// appears as one of its own edges' endpoints: comparing it directly against
/// `src_key`/`dst_key` (as this tool used to) inverts every arrow and swaps
/// the node for its partner.
///
/// There is no public accessor for the canonicalization `edges_at` does
/// internally, so this recovers it from the data instead: the one endpoint
/// every returned edge has in common is the node itself, found by
/// intersecting each edge's `{src_key, dst_key}` (a self-loop contributes
/// just the one key). A single edge to a single partner has nothing to
/// triangulate from — the two endpoints are symmetric from the outside — so
/// `key` is kept as-is in that case, and whenever there are no edges at all
/// (an unrecognized key and a recognized one with nothing at `at` look
/// identical here, matching `edges_at`'s own "unknown key is not an error"
/// contract).
fn canonical_self(edges: &[core_api::EdgeAt], key: &str) -> String {
    let mut candidates: Option<BTreeSet<&str>> = None;
    for e in edges {
        let this_edge: BTreeSet<&str> = if e.src_key == e.dst_key {
            std::iter::once(e.src_key.as_str()).collect()
        } else {
            [e.src_key.as_str(), e.dst_key.as_str()]
                .into_iter()
                .collect()
        };
        candidates = Some(match candidates {
            None => this_edge,
            Some(prev) => prev.intersection(&this_edge).copied().collect(),
        });
    }
    match candidates {
        Some(c) if c.len() == 1 => c.into_iter().next().unwrap().to_string(),
        _ => key.to_string(),
    }
}

fn tool_edges_at(db: &SharedDb, args: &Js, json_out: bool) -> CallOutcome {
    let key = match str_arg(args, "key") {
        Ok(k) => k,
        Err(e) => return CallOutcome::ToolErr(e),
    };
    let at = match args.get("at") {
        None | Some(Js::Null) => return CallOutcome::ToolErr("missing at".into()),
        Some(v) => match v.as_u64() {
            Some(n) => n,
            None => return CallOutcome::ToolErr("at must be a non-negative integer".into()),
        },
    };
    let edge_type = match opt_str_arg(args, "edge_type") {
        Ok(t) => t,
        Err(e) => return CallOutcome::ToolErr(e),
    };
    let all_of = match all_of_arg(args) {
        Ok(t) => t,
        Err(e) => return CallOutcome::ToolErr(e),
    };
    let dir = match partner_dir_arg(args) {
        Ok(d) => d,
        Err(e) => return CallOutcome::ToolErr(e),
    };
    let label = match opt_str_arg(args, "label") {
        Ok(l) => l,
        Err(e) => return CallOutcome::ToolErr(e),
    };
    if !all_of.is_empty() && edge_type.is_some() {
        return CallOutcome::ToolErr(ONE_OF_ALL_OF_OR_EDGE_TYPE.into());
    }
    let keys_only = !all_of.is_empty() || edge_type.is_some();
    let limit = match if keys_only {
        limit_arg(args, DEFAULT_PARTNER_LIMIT, MAX_PARTNER_LIMIT)
    } else {
        edge_limit_arg(args)
    } {
        Ok(n) => n,
        Err(e) => return CallOutcome::ToolErr(e),
    };

    let edges = {
        let g = db.read();
        match g.edges_at(key, at) {
            Ok(v) => v,
            Err(e) => return CallOutcome::ToolErr(crate::mcp::graph_err_msg(e)),
        }
    };
    let self_key = canonical_self(&edges, key);

    if keys_only {
        let mut rows: Vec<PartnerEdge> = edges
            .iter()
            .map(|e| PartnerEdge::of(&e.src_key, &e.dst_key, &e.edge_type, &self_key))
            .collect();
        LabelFilter::new(db, label).retain(&mut rows);
        if !all_of.is_empty() {
            let partners = partners_linked_by_all(&rows, &all_of, dir);
            let report = partners_json(&self_key, Some(at), &all_of, label, &partners, limit);
            return ok(json_out, &report, |_| {
                render_all_of("edges_at", &self_key, Some(at), &all_of, &partners, limit)
            });
        }
        let edge_type = edge_type.unwrap_or_default();
        let (count, partners) = partners_of_type(&rows, edge_type, dir);
        // The rule as it stood at `at`, off the edges themselves — every edge
        // of a type carries the same one, so the first is the type's.
        let rule = edges
            .iter()
            .find(|e| e.edge_type == edge_type && e.rule.is_some())
            .and_then(|e| e.rule.clone());
        let report = with_label(
            type_partners_json(
                &self_key,
                Some(at),
                edge_type,
                rule.as_deref(),
                count,
                &partners,
                limit,
            ),
            label,
        );
        return ok(json_out, &report, |_| {
            let header = format!(
                "mushroomdb edges_at — {} as of commit {at}: {count} edge(s)\n",
                repograph::sanitize(&self_key)
            );
            render_type_partners(header, edge_type, count, rule.as_deref(), &partners, limit)
        });
    }

    // The grouped view, narrowed by `direction` and by the partners carrying
    // `label` if one was named — counts included, so the header says what the
    // filters left. Both apply here exactly as they do on `node_edges`: an
    // argument the tool accepts has to mean the same thing in every form of
    // its reply.
    let mut filter = LabelFilter::new(db, label);
    let edges: Vec<core_api::EdgeAt> = edges
        .into_iter()
        .filter(|e| {
            let outgoing = e.src_key == self_key;
            let other = if outgoing { &e.dst_key } else { &e.src_key };
            match dir {
                Dir::Out if !outgoing => false,
                Dir::In if outgoing => false,
                _ => filter.keeps(other),
            }
        })
        .collect();

    // The report lists what the text lists: at most `limit` per edge type,
    // the engine's own order. Without this an edges_at report of a hub node
    // was a hundred kilobytes of JSON no caller had asked for.
    let mut per_type: BTreeMap<&str, usize> = BTreeMap::new();
    let listed: Vec<Js> = edges
        .iter()
        .filter(|e| {
            let n = per_type.entry(e.edge_type.as_str()).or_default();
            *n += 1;
            *n <= limit
        })
        .map(edge_at_json)
        .collect();
    let report = with_label(
        json!({
            "key": self_key,
            "at": at,
            "edges": listed,
            "listed": listed.len(),
            "total": edges.len(),
        }),
        label,
    );
    let (total, groups) = edges_at_groups(edges, &self_key, limit);
    ok(json_out, &report, |_| {
        render_edges_at(&self_key, at, total, &groups)
    })
}

// ── what_if ──────────────────────────────────────────────────────────────────

/// One edge in a `what_if` text reply. `incident` is `false` for a derived
/// edge the change churns elsewhere in the graph — [`GraphDb::what_if_set_prop`]
/// can report those alongside the ones touching the changed node, and they
/// still need a rule and a partner even though neither endpoint is `key`.
struct WhatIfLine {
    src: String,
    dst: String,
    rule: Option<String>,
    incident: bool,
    outgoing: bool,
}

struct WhatIfGroup {
    edge_type: String,
    count: usize,
    lines: Vec<WhatIfLine>,
}

/// Group `edges` by type, incident edges first within each group — the ones
/// that touch `key` are the answer to the question asked; edges churned
/// elsewhere are secondary and sort after them, in the engine's own order.
fn what_if_groups(edges: &[core_api::EdgeAt], key: &str) -> Vec<WhatIfGroup> {
    let mut by_type: BTreeMap<String, Vec<WhatIfLine>> = BTreeMap::new();
    for e in edges {
        let outgoing = e.src_key == key;
        let incident = outgoing || e.dst_key == key;
        by_type
            .entry(e.edge_type.clone())
            .or_default()
            .push(WhatIfLine {
                src: e.src_key.clone(),
                dst: e.dst_key.clone(),
                rule: e.rule.clone(),
                incident,
                outgoing,
            });
    }
    let mut groups = Vec::with_capacity(by_type.len());
    for (edge_type, mut lines) in by_type {
        // Stable sort: incident edges keep their engine order ahead of the
        // non-incident ones, which keep theirs.
        lines.sort_by_key(|l| !l.incident);
        let count = lines.len();
        groups.push(WhatIfGroup {
            edge_type,
            count,
            lines,
        });
    }
    groups
}

fn render_what_if_groups(out: &mut String, groups: &[WhatIfGroup], limit: usize) {
    if groups.is_empty() {
        out.push_str("  none\n");
        return;
    }
    for g in groups {
        out.push_str(&format!(
            "  {} ({})\n",
            repograph::sanitize(&g.edge_type),
            g.count
        ));
        for l in g.lines.iter().take(limit) {
            if l.incident {
                let arrow = if l.outgoing { "→" } else { "←" };
                let other = if l.outgoing { &l.dst } else { &l.src };
                out.push_str(&format!("    {arrow} {}", repograph::sanitize(other)));
            } else {
                out.push_str(&format!(
                    "    {} → {}",
                    repograph::sanitize(&l.src),
                    repograph::sanitize(&l.dst)
                ));
            }
            if let Some(rule) = &l.rule {
                out.push_str(&format!("  rule {}", repograph::sanitize(rule)));
            }
            out.push('\n');
        }
        if g.count > limit {
            out.push_str(&format!("    … and {} more\n", g.count - limit));
        }
    }
}

fn render_what_if(
    key: &str,
    field: &str,
    value: &Js,
    lost: &[WhatIfGroup],
    gained: &[WhatIfGroup],
    limit: usize,
) -> String {
    let edges = |gs: &[WhatIfGroup]| gs.iter().map(|g| g.count).sum::<usize>();
    let mut out = what_if_header(key, field, value, edges(lost), edges(gained));
    out.push_str("lost\n");
    render_what_if_groups(&mut out, lost, limit);
    out.push_str("gained\n");
    render_what_if_groups(&mut out, gained, limit);
    // No line budget on top of `limit`. This reply has two sections, and a
    // budget that ran out inside the first one deleted the second without
    // saying so: at `limit: 50` with fifty lost edges, the whole `gained`
    // section — heading included — fell off the end of a reply that had just
    // said how many there were. `limit` is the cap here, and every group that
    // it cuts says `… and N more` itself.
    out
}

fn what_if_header(
    key: &str,
    field: &str,
    value: &Js,
    lost_total: usize,
    gained_total: usize,
) -> String {
    format!(
        "mushroomdb what_if — {}.{} = {}: would lose {lost_total}, would gain {gained_total}\n",
        repograph::sanitize(key),
        repograph::sanitize(field),
        repograph::sanitize(&value.to_string()),
    )
}

/// The distinct nodes one side of a `what_if` touches, sorted.
///
/// An edge incident on the changed node is named by its partner — the answer
/// to "which partners does this cost me". An edge the change churns elsewhere
/// in the graph has no partner to name, so it is written out as the pair it
/// is, rather than being dropped from a reply that counts it.
fn what_if_partners(edges: &[core_api::EdgeAt], key: &str) -> Vec<String> {
    let mut set: BTreeSet<String> = BTreeSet::new();
    for e in edges {
        if e.src_key == key {
            set.insert(e.dst_key.clone());
        } else if e.dst_key == key {
            set.insert(e.src_key.clone());
        } else {
            set.insert(format!("{} → {}", e.src_key, e.dst_key));
        }
    }
    set.into_iter().collect()
}

/// The keys-only `what_if` reply: one edge type, so one rule, so two lists of
/// keys under `lost` and `gained`.
fn render_what_if_keys_only(
    key: &str,
    field: &str,
    value: &Js,
    edge_type: &str,
    lost: &[core_api::EdgeAt],
    gained: &[core_api::EdgeAt],
    limit: usize,
) -> String {
    let mut out = what_if_header(key, field, value, lost.len(), gained.len());
    for (heading, side) in [("lost\n", lost), ("gained\n", gained)] {
        out.push_str(heading);
        let partners = what_if_partners(side, key);
        if partners.is_empty() {
            out.push_str("  none\n");
            continue;
        }
        let rule = side
            .iter()
            .find_map(|e| e.rule.as_deref())
            .map_or_else(String::new, |r| {
                format!(", rule {}", repograph::sanitize(r))
            });
        let lead = format!("{} ({}{rule}):", repograph::sanitize(edge_type), side.len());
        push_partner_block(&mut out, &lead, &partners, limit);
    }
    out
}

/// What changes if `key.field` became `value` — computed directly by the
/// engine ([`GraphDb::what_if_set_prop`]) without writing anything: no copy
/// of the store is made, so nothing here needs to know where it lives on
/// disk.
fn tool_what_if(db: &SharedDb, args: &Js, json_out: bool) -> CallOutcome {
    let key = match str_arg(args, "key") {
        Ok(k) => k,
        Err(e) => return CallOutcome::ToolErr(e),
    };
    let field = match str_arg(args, "field") {
        Ok(f) => f,
        Err(e) => return CallOutcome::ToolErr(e),
    };
    let Some(raw) = args.get("value").filter(|v| !v.is_null()) else {
        return CallOutcome::ToolErr("missing value".into());
    };
    let Some(value) = json_to_value(raw.clone()) else {
        return CallOutcome::ToolErr(format!(
            "value is not a supported value type: {}",
            repograph::sanitize(&raw.to_string())
        ));
    };

    let edge_type = match opt_str_arg(args, "edge_type") {
        Ok(t) => t,
        Err(e) => return CallOutcome::ToolErr(e),
    };
    let label = match opt_str_arg(args, "label") {
        Ok(l) => l,
        Err(e) => return CallOutcome::ToolErr(e),
    };
    let limit = match limit_arg(args, DEFAULT_EDGE_LIMIT, MAX_PARTNER_LIMIT) {
        Ok(n) => n,
        Err(e) => return CallOutcome::ToolErr(e),
    };

    let wi = {
        let g = db.read();
        g.what_if_set_prop(key, field, value)
    };
    let wi = match wi {
        Ok(w) => w,
        Err(e) => return CallOutcome::ToolErr(crate::mcp::graph_err_msg(e)),
    };

    // `edge_type` and `label` narrow what is counted as well as what is
    // listed: the reply is then about that type, or those partners, totals
    // included. An edge the change churns elsewhere in the graph has no
    // partner to carry a label, so a labelled call leaves it out.
    let mut filter = LabelFilter::new(db, label);
    let mut pick = |edges: &'_ [core_api::EdgeAt]| -> Vec<core_api::EdgeAt> {
        edges
            .iter()
            .filter(|e| edge_type.is_none_or(|t| e.edge_type == t))
            .filter(|e| match (e.src_key == key, e.dst_key == key) {
                (true, _) => filter.keeps(&e.dst_key),
                (_, true) => filter.keeps(&e.src_key),
                _ => label.is_none(),
            })
            .cloned()
            .collect()
    };
    let lost = pick(&wi.lost);
    let gained = pick(&wi.gained);

    let doc = |edges: &[core_api::EdgeAt]| -> Vec<Js> {
        edges
            .iter()
            .take(limit)
            .map(edge_at_json)
            .collect::<Vec<_>>()
    };
    let mut report = with_label(
        json!({
            "key": key,
            "field": field,
            "value": raw,
            "lost": doc(&lost),
            "lost_total": lost.len(),
            "gained": doc(&gained),
            "gained_total": gained.len(),
        }),
        label,
    );
    if let Some(t) = edge_type {
        report["edge_type"] = json!(t);
    }

    if let Some(t) = edge_type {
        return ok(json_out, &report, |_| {
            render_what_if_keys_only(key, field, raw, t, &lost, &gained, limit)
        });
    }
    let lost_groups = what_if_groups(&lost, key);
    let gained_groups = what_if_groups(&gained, key);
    ok(json_out, &report, |_| {
        render_what_if(key, field, raw, &lost_groups, &gained_groups, limit)
    })
}

// ── recall ───────────────────────────────────────────────────────────────────

fn tool_recall(db: &SharedDb, db_dir: Option<&Path>, args: &Js, json_out: bool) -> CallOutcome {
    let topic = match str_arg(args, "topic") {
        Ok(t) => t.to_string(),
        Err(e) => return CallOutcome::ToolErr(e),
    };
    let label = db_dir.map_or_else(|| "store".to_string(), |d| d.display().to_string());
    // The topic goes in as the caller wrote it: `recall_digest` searches the
    // identifiers in it, and it is the same call the `recall` hook makes, so
    // the two cannot disagree about what a topic means.
    let digest = {
        let g = db.read();
        repograph::recall_digest(&*g, &topic, &label, MAX_OUTPUT_BYTES)
    };
    let text = if digest.is_empty() {
        format!(
            "mushroomdb recall — nothing indexed matches {}\n",
            repograph::sanitize(&topic)
        )
    } else {
        digest.clone()
    };
    ok(
        json_out,
        &json!({ "topic": topic, "digest": digest }),
        |_| text,
    )
}

// ── remember ─────────────────────────────────────────────────────────────────

fn tool_remember(db: &SharedDb, args: &Js, json_out: bool) -> CallOutcome {
    let text = match str_arg(args, "text") {
        Ok(t) => t.to_string(),
        Err(e) => return CallOutcome::ToolErr(e),
    };
    let mut about = match str_list_arg(args, "about") {
        Ok(a) => a,
        Err(e) => return CallOutcome::ToolErr(e),
    };
    about.sort();
    about.dedup();
    let kind = match args.get("kind") {
        None | Some(Js::Null) => "note".to_string(),
        Some(Js::String(k)) => k.clone(),
        Some(_) => return CallOutcome::ToolErr("kind must be a string".into()),
    };
    if !NOTE_KINDS.contains(&kind.as_str()) {
        return CallOutcome::ToolErr(format!(
            "kind must be one of {}, got {kind:?}",
            NOTE_KINDS.join(", ")
        ));
    }

    // The engine names the first missing key, which makes a caller with three
    // bad ones retry three times. Check them all here and name them all at
    // once, before anything is written.
    let missing: Vec<String> = {
        let g = db.read();
        about
            .iter()
            .filter(|k| !g.has_node(k))
            .map(|k| repograph::sanitize(k))
            .collect()
    };
    if !missing.is_empty() {
        return CallOutcome::ToolErr(format!(
            "unknown about {}: {}",
            if missing.len() == 1 { "key" } else { "keys" },
            missing.join(", ")
        ));
    }

    let ts = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map_or(0, |d| d.as_secs() as i64);
    let input = RememberInput {
        text: &text,
        about: &about,
        kind: &kind,
        ts,
    };
    let key = {
        let mut g = db.write();
        repograph::remember(&mut *g, &input)
    };
    match key {
        Ok(key) => {
            let mut rendered = format!("remembered {}\n", repograph::sanitize(&key));
            if !about.is_empty() {
                rendered.push_str(&format!(
                    "about  {}\n",
                    about
                        .iter()
                        .map(|k| repograph::sanitize(k))
                        .collect::<Vec<_>>()
                        .join(", ")
                ));
            }
            ok(
                json_out,
                &json!({ "key": key, "kind": kind, "about": about }),
                |_| rendered,
            )
        }
        Err(e) => CallOutcome::ToolErr(match e {
            GraphError::QueryError { detail } | GraphError::IngestError { detail } => detail,
            other => other.to_string(),
        }),
    }
}

// ── sync ─────────────────────────────────────────────────────────────────────

/// Run the incremental ingest and report what it did.
///
/// The child is waited on to completion. A full sync of a large repository is
/// real work, and an assistant that asked for one is waiting on the answer;
/// cutting it off part-way would leave the store half-updated with nothing said
/// about it. The MCP loop is single-threaded, so nothing else is served while
/// it runs — which is correct, since every other tool would be answering from
/// the store the child is rewriting.
fn tool_sync(db_dir: Option<&Path>, json_out: bool) -> CallOutcome {
    let Some(db_dir) = db_dir else {
        return CallOutcome::ToolErr(
            "store path unknown: sync needs the directory this server was started on".into(),
        );
    };
    let exe = match std::env::current_exe() {
        Ok(exe) => exe,
        Err(e) => return CallOutcome::ToolErr(format!("sync cannot find this binary: {e}")),
    };
    // The incremental ingest lives in the CLI crate, which the server cannot
    // depend on, so `sync` re-runs this same binary. Under the npx launcher
    // `current_exe()` is already the native binary rather than the shim.
    let output = match Command::new(&exe)
        .arg("sync")
        .arg(db_dir)
        .arg("--json")
        .output()
    {
        Ok(o) => o,
        Err(e) => {
            return CallOutcome::ToolErr(format!("sync could not run {}: {e}", exe.display()))
        }
    };
    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        let detail = stderr.trim();
        let detail = if detail.is_empty() {
            format!("exit {}", output.status)
        } else {
            repograph::sanitize(detail)
        };
        return CallOutcome::ToolErr(format!("sync failed: {detail}"));
    }
    let stdout = String::from_utf8_lossy(&output.stdout);
    let Ok(Js::Object(report)) = serde_json::from_str::<Js>(stdout.trim()) else {
        return CallOutcome::ToolErr(format!(
            "sync produced no report: {}",
            repograph::sanitize(stdout.trim())
        ));
    };
    // The CLI already rendered the digest into the object, so the digest and
    // the numbers come from the one run whichever the caller asked for.
    let text = report
        .get("text")
        .and_then(Js::as_str)
        .unwrap_or_default()
        .to_string();
    ok(json_out, &Js::Object(report), |_| text)
}

// ── tools/list ───────────────────────────────────────────────────────────────

/// The `json` argument every task tool takes, added to all ten schemas by
/// [`task_tools`] rather than written out ten times.
fn json_arg() -> Js {
    json!({
        "type": "boolean",
        "description": "Answer with the report as JSON, not the rendered digest."
    })
}

/// The ten task tools, in the order `tools/list` puts them: the question an
/// assistant asks first comes first.
pub(crate) fn task_tools() -> Vec<Js> {
    let mut tools = task_tool_schemas();
    for tool in &mut tools {
        if let Some(props) = tool["inputSchema"]["properties"].as_object_mut() {
            props.insert("json".to_string(), json_arg());
        }
    }
    tools
}

fn task_tool_schemas() -> Vec<Js> {
    vec![
        json!({
            "name": "explore",
            "description": "Find your way around this repository from its code graph: a symbol's definition, callers and callees; the blast radius (files that import it or change with it) if it changes; who owns it and why files are related. Cheaper than grep for anything cross-file. depth=context (default) | impact | history | all.",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "target": {
                        "type": "string",
                        "minLength": 1,
                        "description": "A file path, a symbol key (path#name), or a bare symbol name."
                    },
                    "depth": {
                        "type": "string",
                        "enum": ["context", "impact", "history", "all"]
                    },
                    "budget": {
                        "type": "integer",
                        "minimum": 200,
                        "description": "Max reply tokens (default 1200)."
                    },
                    "full": {
                        "type": "boolean",
                        "description": "Include the source body."
                    }
                },
                "required": ["target"]
            }
        }),
        json!({
            "name": "map",
            "description": "Summarise the graphed repository in one screen: size, last sync, clusters, key files, owners, hot files, stale concepts, and questions worth asking next. Start here when you do not know the codebase.",
            "inputSchema": { "type": "object", "properties": {} }
        }),
        json!({
            "name": "context",
            "description": "Everything known about one file or symbol: where it is as path:start-end, its signature and doc, owner, every call site into it grouped by calling file, its callees, importers and imports, co-change partners, recent commits, and any notes or concepts about it. The body is not quoted unless you ask for it with 'full'.",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "target": {
                        "type": "string",
                        "minLength": 1,
                        "description": "A file path, a symbol key (path#name), or a bare symbol name. An ambiguous bare name returns the candidates instead."
                    },
                    "full": {
                        "type": "boolean",
                        "description": "Include the source body (default: pointers and signature only)."
                    }
                },
                "required": ["target"]
            }
        }),
        json!({
            "name": "impact",
            "description": "What else the files in a change reach: co-change partners, by similarity score or by how many commits the two share, plus importers, symbols used elsewhere, and each file's owner. Defaults to the current git diff plus untracked files when no list is given.",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "files": {
                        "type": "array",
                        "items": { "type": "string" },
                        "description": "Repository-relative paths. Omit to use the working tree's diff against HEAD plus its untracked files."
                    }
                }
            }
        }),
        json!({
            "name": "owners",
            "description": "Who has written a file: top author and share, authors who know it, the last commit to touch it, and the split by quarter.",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "path": {
                        "type": "string",
                        "minLength": 1,
                        "description": "Repository-relative file path."
                    }
                },
                "required": ["path"]
            }
        }),
        json!({
            "name": "why",
            "description": "What links two files, symbols, or people, with the evidence for each link: shared commits, the importing line, every calling line, the file two authors both know. With no rule edge it reports the commits the two share, and failing that the shortest path between them.",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "a": { "type": "string", "minLength": 1, "description": "First node key." },
                    "b": { "type": "string", "minLength": 1, "description": "Second node key." }
                },
                "required": ["a", "b"]
            }
        }),
        json!({
            "name": "explain_association",
            "description": "Why are A and B related — every relationship between the two keys and its evidence: the rule that derived it, its edge type, the match score, the predicate it matched on, and the values the two actually share (which specialties overlapped, which field was equal, how far apart they are). Answer from those shared values; the nodes' full property lists name everything either one holds, not what they have in common. Both keys must already exist.",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "a": { "type": "string", "minLength": 1, "description": "First node key." },
                    "b": { "type": "string", "minLength": 1, "description": "Second node key." }
                },
                "required": ["a", "b"]
            }
        }),
        json!({
            "name": "node_edges",
            "description": "What is K related to — every relationship of one node, grouped by edge type with a count, each listed edge carrying its direction, the rule that derived it, its score and the predicate it matched on. Answers 'why is this here' in the same call that lists it, so no follow-up explain is needed. Which partners are linked by all of these types? pass all_of and the reply is just their keys; pass one edge_type for that type's partner keys with the rule named once. label narrows partners. With json:true the report carries `listed` and `total`, so a reply that was cut still says how much there was.",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "key": { "type": "string", "minLength": 1, "description": "Node key." },
                    "edge_type": {
                        "type": "string",
                        "minLength": 1,
                        "description": "Only this type: the reply is that type's partner keys, compactly. Omit for the grouped listing over every type."
                    },
                    "all_of": {
                        "type": "array",
                        "items": { "type": "string" },
                        "minItems": 1,
                        "description": "Only the partners linked by EVERY one of these types — the intersection, as keys."
                    },
                    "label": {
                        "type": "string",
                        "minLength": 1,
                        "description": "Only partners carrying this node label, counts included."
                    },
                    "direction": {
                        "type": "string",
                        "enum": ["out", "in", "any"],
                        "description": "Which edges count (default any)."
                    },
                    "limit": {
                        "type": "integer",
                        "minimum": 1,
                        "maximum": 2000,
                        "description": "Edges listed per edge type (default 10, max 100), or partner keys listed under edge_type/all_of (default 200, max 2000). The rest are counted as '… and N more'."
                    }
                },
                "required": ["key"]
            }
        }),
        json!({
            "name": "neighborhood",
            "description": "What is around K — one hop out, as the same grouped relationship listing node_edges gives, with the rule and score behind each edge. With depth above 1 it is the breadth-first table of (key, label, depth) instead, because past one hop no single rule accounts for a row. label narrows the reply to nodes carrying it, at either depth.",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "key": { "type": "string", "minLength": 1, "description": "Node key to start from." },
                    "depth": {
                        "type": "integer",
                        "minimum": 1,
                        "description": "Hops to traverse (default 1). 1 gives the relationship listing; above 1 gives the traversal table."
                    },
                    "edge_types": {
                        "type": "array",
                        "items": { "type": "string" },
                        "description": "Only follow these edge types. Omit for every type."
                    },
                    "label": {
                        "type": "string",
                        "minLength": 1,
                        "description": "Only nodes carrying this label: at depth 1 the partners, counts included; above it the rows of the traversal table. The walk itself is never narrowed — a hop through another label is how the label you asked about is reached."
                    },
                    "direction": {
                        "type": "string",
                        "enum": ["out", "in", "both"],
                        "description": "Edge direction to follow (default both)."
                    },
                    "limit": {
                        "type": "integer",
                        "minimum": 1,
                        "maximum": 100,
                        "description": "At depth 1, edges listed per edge type (default 10)."
                    }
                },
                "required": ["key"]
            }
        }),
        json!({
            "name": "edges_at",
            "description": "What did K's relationships look like at commit C — the edges that were live at one point in the store's history, with the rule that had derived each. `at` is a 0-based WAL commit index; use node_history or edge_history first to find the commit you want, then read this instead of replaying either by hand. Which partners were linked by all of these types on that day? pass all_of and the reply is just their keys; pass one edge_type for that type's partner keys with the rule named once. label narrows partners. With json:true the report carries `listed` and `total`, so a reply that was cut still says how much there was.",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "key": { "type": "string", "minLength": 1, "description": "Node key." },
                    "at": {
                        "type": "integer",
                        "minimum": 0,
                        "description": "0-based WAL commit index to read the edges at."
                    },
                    "edge_type": {
                        "type": "string",
                        "minLength": 1,
                        "description": "Only this type: the reply is that type's partner keys, compactly. Omit for the grouped listing over every type."
                    },
                    "all_of": {
                        "type": "array",
                        "items": { "type": "string" },
                        "minItems": 1,
                        "description": "Only the partners linked by EVERY one of these types at that commit — the intersection, as keys."
                    },
                    "label": {
                        "type": "string",
                        "minLength": 1,
                        "description": "Only partners carrying this node label, counts included."
                    },
                    "direction": {
                        "type": "string",
                        "enum": ["out", "in", "any"],
                        "description": "Which edges count (default any)."
                    },
                    "limit": {
                        "type": "integer",
                        "minimum": 1,
                        "maximum": 2000,
                        "description": "Edges listed per edge type (default 10, max 100), or partner keys listed under edge_type/all_of (default 200, max 2000). The rest are counted as '… and N more'."
                    }
                },
                "required": ["key", "at"]
            }
        }),
        json!({
            "name": "what_if",
            "description": "What changes if K's FIELD became VALUE — the relationships lost and gained, with the rule behind each. Does not change the store: nothing is written, nothing on disk is copied, and the live graph answers the same way before and after the call. Which partners of one type would it cost? pass edge_type and the lost and gained lists are just their keys, with the rule named once. label narrows partners.",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "key": { "type": "string", "minLength": 1, "description": "Node key to change." },
                    "field": { "type": "string", "minLength": 1, "description": "Property name to set." },
                    "value": {
                        "description": "The value it would take: a string, number, boolean, or a list or map of those. Not null."
                    },
                    "edge_type": {
                        "type": "string",
                        "minLength": 1,
                        "description": "Only this type, counts included: the reply is the partner keys lost and gained, compactly."
                    },
                    "label": {
                        "type": "string",
                        "minLength": 1,
                        "description": "Only partners carrying this node label, counts included."
                    },
                    "limit": {
                        "type": "integer",
                        "minimum": 1,
                        "maximum": 2000,
                        "description": "Edges (or partner keys) listed per side (default 10). The rest are counted as '… and N more'."
                    }
                },
                "required": ["key", "field", "value"]
            }
        }),
        json!({
            "name": "recall",
            "description": "What do I already know about this — where the graph says a topic lives: one pointer per hit, path:line, the symbol, and the first line of its doc, across notes, concepts, files, symbols and people.",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "topic": {
                        "type": "string",
                        "minLength": 1,
                        "description": "Free-form text. The identifiers in it — a path, a `mod::name`, a snake_case word, or any word in backticks — are searched as phrases; a topic naming none of those matches nothing."
                    }
                },
                "required": ["topic"]
            }
        }),
        json!({
            "name": "remember",
            "description": "Remember this for next time — write a note into the graph and return its key. Keys listed in 'about' are linked to the note, and every one of them must already exist.",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "text": {
                        "type": "string",
                        "minLength": 1,
                        "description": "The note itself, 1 to 4000 characters."
                    },
                    "about": {
                        "type": "array",
                        "items": { "type": "string" },
                        "description": "Existing node keys the note is about: files, symbols, authors, concepts, other notes."
                    },
                    "kind": {
                        "type": "string",
                        "enum": ["note", "decision", "todo"],
                        "description": "What kind of note this is (default: note)."
                    }
                },
                "required": ["text"]
            }
        }),
        json!({
            "name": "sync",
            "description": "Bring the store up to date with the repository it was built from: the commits since the last sync, then the files that differ from HEAD. Returns what changed.",
            "inputSchema": { "type": "object", "properties": {} }
        }),
    ]
}

// ─────────────────────────────────────────────────────────────────────────────
// Tests: the two things these tools decide before they touch the graph — where
// a default `impact` reads its diff from, and how that diff is filtered.
//
// They live here rather than in `tests/mcp.rs` because `$CLAUDE_PROJECT_DIR`
// reaches `tool_impact` as an argument, not as a process-global read: setting
// it for real would race every other test in the binary that calls
// `std::env::temp_dir()`.
// ─────────────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use core_api::Value;
    use std::sync::atomic::{AtomicU64, Ordering};

    fn tmp(name: &str) -> PathBuf {
        static SEQ: AtomicU64 = AtomicU64::new(0);
        let n = SEQ.fetch_add(1, Ordering::Relaxed);
        let d = std::env::temp_dir().join(format!("mcp-tasks-{name}-{}-{n}", std::process::id()));
        let _ = std::fs::remove_dir_all(&d);
        d
    }

    fn git(repo: &Path, args: &[&str]) {
        let out = Command::new("git")
            .arg("-C")
            .arg(repo)
            .args(args)
            .output()
            .expect("git");
        assert!(out.status.success(), "git {args:?}: {out:?}");
    }

    /// A checkout holding one committed file, since edited, plus one untracked
    /// file under an excluded directory.
    fn dirty_repo(name: &str) -> PathBuf {
        let repo = tmp(name);
        std::fs::create_dir_all(repo.join("src")).expect("src");
        std::fs::create_dir_all(repo.join("target")).expect("target");
        git(&repo, &["init", "-q"]);
        git(&repo, &["config", "user.email", "t@example.test"]);
        git(&repo, &["config", "user.name", "Test"]);
        std::fs::write(repo.join("src/core.rs"), "fn init() {}\n").expect("write");
        git(&repo, &["add", "src/core.rs"]);
        git(&repo, &["commit", "-qm", "first"]);
        std::fs::write(repo.join("src/core.rs"), "fn init() { /* edited */ }\n").expect("edit");
        // Untracked and excluded at ingest time, so it must not reach the list.
        std::fs::write(repo.join("target/debug.log"), "noise\n").expect("artefact");
        repo
    }

    /// A store holding one `File` node and a `GitSync` marker pointing at `repo`.
    fn store_for(name: &str, repo: Option<&Path>) -> (SharedDb, PathBuf) {
        let dir = tmp(name);
        let db = SharedDb::open(&dir).expect("open");
        {
            let mut w = db.write();
            w.insert_node(
                "File",
                "src/core.rs",
                vec![
                    ("id".into(), Value::Str("src/core.rs".into())),
                    ("path".into(), Value::Str("src/core.rs".into())),
                    ("lines".into(), Value::Int(1)),
                ],
            )
            .expect("file");
            let marker = repo.map_or_else(
                || "/nonexistent/mushroomdb-test-repo".to_string(),
                |r| r.display().to_string(),
            );
            w.insert_node(
                "GitSync",
                SYNC_KEY,
                vec![
                    ("id".into(), Value::Str(SYNC_KEY.into())),
                    (SYNC_REPO_PROP.into(), Value::Str(marker)),
                ],
            )
            .expect("marker");
        }
        (db, dir)
    }

    /// The report behind a `json: true` reply, which is now the only place a
    /// caller reads the numbers from: the text content *is* the JSON.
    fn report(outcome: &CallOutcome) -> Js {
        match outcome {
            CallOutcome::TaskOk { text } => {
                serde_json::from_str(text).expect("a json reply is the serialised report")
            }
            other => panic!("expected a task result, got {}", describe(other)),
        }
    }

    fn impact_files(outcome: &CallOutcome) -> Vec<String> {
        report(outcome)["files"]
            .as_array()
            .expect("files")
            .iter()
            .map(|f| f["path"].as_str().expect("path").to_string())
            .collect()
    }

    /// `impact` with no `files`, asking for the report rather than the digest.
    fn impact_report(db: &SharedDb, project_dir: Option<&OsStr>) -> CallOutcome {
        tool_impact(db, &json!({"json": true}), project_dir, true)
    }

    fn describe(outcome: &CallOutcome) -> String {
        match outcome {
            CallOutcome::ToolErr(m) => format!("tool error: {m}"),
            CallOutcome::TaskOk { text } => format!("ok: {text}"),
            CallOutcome::ToolOk(v) => format!("json: {v}"),
            CallOutcome::Protocol { message, .. } => format!("protocol: {message}"),
        }
    }

    /// Binding: with no `files`, the diff comes from the checkout the marker
    /// names, and excluded artefacts are left out of it.
    #[test]
    fn default_files_come_from_the_marker_repo_and_skip_excluded_paths() {
        let repo = dirty_repo("marker-repo");
        let (db, dir) = store_for("marker-store", Some(&repo));

        let outcome = impact_report(&db, None);
        assert_eq!(
            impact_files(&outcome),
            vec!["src/core.rs".to_string()],
            "the uncommitted edit, and not the build artefact"
        );
        assert_eq!(
            report(&outcome)["unknown"],
            json!([]),
            "an excluded path must not come back as unknown"
        );

        drop(db);
        let _ = std::fs::remove_dir_all(&dir);
        let _ = std::fs::remove_dir_all(&repo);
    }

    /// Binding: `$CLAUDE_PROJECT_DIR` wins over the marker when it names a
    /// checkout.
    #[test]
    fn the_project_directory_wins_over_the_marker() {
        let project = dirty_repo("project-repo");
        // The marker points somewhere that does not exist, so a result at all
        // proves the project directory was the one read.
        let (db, dir) = store_for("project-store", None);

        let outcome = impact_report(&db, Some(project.as_os_str()));
        assert_eq!(impact_files(&outcome), vec!["src/core.rs".to_string()]);

        drop(db);
        let _ = std::fs::remove_dir_all(&dir);
        let _ = std::fs::remove_dir_all(&project);
    }

    /// Binding: a subdirectory of a checkout resolves to the checkout root, so
    /// both git listings agree about what their paths are relative to.
    #[test]
    fn a_project_subdirectory_resolves_to_the_repository_root() {
        let repo = dirty_repo("subdir-repo");
        let (db, dir) = store_for("subdir-store", None);

        let outcome = impact_report(&db, Some(repo.join("src").as_os_str()));
        assert_eq!(
            impact_files(&outcome),
            vec!["src/core.rs".to_string()],
            "paths stay root-relative, matching File keys"
        );

        drop(db);
        let _ = std::fs::remove_dir_all(&dir);
        let _ = std::fs::remove_dir_all(&repo);
    }

    /// Binding: a project directory that is not inside a checkout says nothing
    /// about the store's repository, so the marker still answers.
    #[test]
    fn a_project_directory_outside_a_checkout_falls_back_to_the_marker() {
        let repo = dirty_repo("fallback-repo");
        let plain = tmp("fallback-plain");
        std::fs::create_dir_all(&plain).expect("plain dir");
        let (db, dir) = store_for("fallback-store", Some(&repo));

        let outcome = impact_report(&db, Some(plain.as_os_str()));
        assert_eq!(impact_files(&outcome), vec!["src/core.rs".to_string()]);

        drop(db);
        let _ = std::fs::remove_dir_all(&dir);
        let _ = std::fs::remove_dir_all(&plain);
        let _ = std::fs::remove_dir_all(&repo);
    }

    /// Binding: with neither a project checkout nor a marker checkout, the tool
    /// says what the caller must do instead.
    #[test]
    fn no_checkout_anywhere_says_pass_files_explicitly() {
        let (db, dir) = store_for("no-repo-store", None);

        let outcome = impact_report(
            &db,
            Some(OsStr::new("/nonexistent/mushroomdb-test-project")),
        );
        match &outcome {
            CallOutcome::ToolErr(m) => assert!(m.contains("pass files explicitly"), "{m}"),
            other => panic!("{}", describe(other)),
        }

        drop(db);
        let _ = std::fs::remove_dir_all(&dir);
    }

    /// Binding: an explanation's line carries the score and the hop a via-rule
    /// went over, and the digest never runs past the line budget.
    ///
    /// `tests/mcp.rs` covers the plain rule and the empty case end to end; what
    /// is only reachable from here is a via-hop rule and a report longer than
    /// [`repograph::MAX_TOOL_LINES`], neither of which a two-node fixture
    /// produces.
    #[test]
    fn an_explanation_line_names_the_score_the_hop_and_the_predicate() {
        let one = |rule: &str, via: Option<&str>| ExplainedEdge {
            edge: Explanation {
                rule: rule.to_string(),
                edge_type: "SIMILAR".to_string(),
                src_key: "a".to_string(),
                dst_key: "b".to_string(),
                weight: Some(0.9625),
                predicate: PredicateSummary {
                    kind: "vector_similar".to_string(),
                    fields: vec!["emb".to_string()],
                    min: Some(0.85),
                    tolerance: None,
                    km: None,
                    parts: None,
                    approximate: false,
                },
                via_edge: via.map(str::to_string),
            },
            // A via-hop rule matched between the via node and the
            // destination, so there is no pair here to show evidence from.
            evidence: None,
        };

        let text = render_explanations("a", "b", &[one("close", Some("WORKS_AT"))]);
        assert_eq!(
            text,
            "mushroomdb explain — a ↔ b: 1 relationship(s)\n  SIMILAR via rule close (score 0.96) \
             via WORKS_AT — vector_similar on emb >= 0.85\n"
        );

        let many: Vec<ExplainedEdge> = (0..40).map(|i| one(&format!("r{i}"), None)).collect();
        let capped = render_explanations("a", "b", &many);
        assert_eq!(
            capped.lines().count(),
            repograph::MAX_TOOL_LINES,
            "the digest is capped like every other one"
        );
        assert!(
            capped.starts_with("mushroomdb explain — a ↔ b: 40 relationship(s)"),
            "and the header still says how many there were: {capped}"
        );
    }

    /// Binding: an explicit `files` list never looks at a repository at all.
    #[test]
    fn explicit_files_ignore_the_project_directory() {
        let (db, dir) = store_for("explicit-store", None);

        let outcome = tool_impact(
            &db,
            &json!({"files": ["src/core.rs"], "json": true}),
            Some(OsStr::new("/nonexistent/mushroomdb-test-project")),
            true,
        );
        assert_eq!(impact_files(&outcome), vec!["src/core.rs".to_string()]);

        drop(db);
        let _ = std::fs::remove_dir_all(&dir);
    }

    fn edge_at(edge_type: &str, src: &str, dst: &str) -> core_api::EdgeAt {
        core_api::EdgeAt {
            edge_type: edge_type.into(),
            src_key: src.into(),
            dst_key: dst.into(),
            derived: false,
            rule: None,
        }
    }

    /// Binding: `canonical_self` recovers the node's identity by intersecting
    /// the endpoints of every edge it appears in — the fix for `edges_at`
    /// comparing a stale alias against the engine's already-canonicalized
    /// endpoints (which would otherwise invert every arrow and report the
    /// node as its own partner).
    #[test]
    fn canonical_self_intersects_endpoints_across_edges() {
        // Two edges to two different partners narrow to exactly one shared
        // endpoint: the node itself, even though `key` ("old") never appears
        // in either edge — this is what querying `edges_at` by a stale alias
        // of a renamed node looks like once the engine has canonicalized the
        // output to the current key ("new").
        let edges = vec![edge_at("Knows", "new", "p1"), edge_at("Knows", "p2", "new")];
        assert_eq!(canonical_self(&edges, "old"), "new");

        // A single edge to a single partner is symmetric — there is nothing
        // to triangulate from — so the raw key is kept rather than guessed.
        let one_edge = vec![edge_at("Knows", "new", "p1")];
        assert_eq!(canonical_self(&one_edge, "old"), "old");

        // The common, non-renamed case: `key` already appears in the edges,
        // so it is returned unchanged even when candidates cannot narrow to
        // one (self-loop aside, this is the fallback path's every-day case).
        let unrenamed = vec![edge_at("Knows", "a", "b")];
        assert_eq!(canonical_self(&unrenamed, "a"), "a");

        // No edges at all: nothing to recover from, `key` is kept as-is.
        assert_eq!(canonical_self(&[], "whatever"), "whatever");

        // A self-loop contributes just the one key to the candidate set.
        let self_loop = vec![
            edge_at("Knows", "new", "new"),
            edge_at("Likes", "new", "p1"),
        ];
        assert_eq!(canonical_self(&self_loop, "old"), "new");
    }
}