mnem-cli 0.1.6

Command-line interface for mnem - git for knowledge graphs.
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
//! `mnem integrate` - wire mnem into every agent host on the machine.
//!
//! Detects installed MCP-aware agent hosts (Claude Desktop, Cursor,
//! Continue, Zed) by probing platform-specific config paths. Merges a
//! `mnem` MCP-server entry into each selected host's config with an
//! atomic temp-file-plus-rename after a timestamped backup. Never
//! overwrites other MCP entries.

use std::fs;
use std::io::Write as _;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result, anyhow};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value, json};

// ---------- Integration registry ----------

/// One per integrated host, written to `~/.mnemglobal/integrations.toml`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct IntegrationRecord {
    pub slug: String,
    pub display: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub config_path: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hooks_path: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub system_prompt_path: Option<String>,
    /// Epoch-seconds timestamp of the last successful integrate.
    pub integrated_at: u64,
    /// Which components were wired: "mcp", "hooks", "system_prompt".
    #[serde(default)]
    pub components: Vec<String>,
}

#[derive(Debug, Default, Serialize, Deserialize)]
pub(crate) struct IntegrationRegistry {
    #[serde(default)]
    pub hosts: Vec<IntegrationRecord>,
}

impl IntegrationRegistry {
    pub(crate) fn load() -> Self {
        let path = global_dir().join("integrations.toml");
        let text = match std::fs::read_to_string(&path) {
            Ok(t) => t,
            Err(_) => return Self::default(),
        };
        toml::from_str(&text).unwrap_or_default()
    }

    fn save(&self) -> Result<()> {
        let dir = global_dir();
        std::fs::create_dir_all(&dir).with_context(|| format!("creating {}", dir.display()))?;
        let text = toml::to_string_pretty(self).context("serialising integrations.toml")?;
        let path = dir.join("integrations.toml");
        atomic_write(&path, &text)?;
        Ok(())
    }

    /// Upsert a record for `host`.
    pub(crate) fn upsert(&mut self, record: IntegrationRecord) {
        if let Some(existing) = self.hosts.iter_mut().find(|r| r.slug == record.slug) {
            *existing = record;
        } else {
            self.hosts.push(record);
        }
    }

    /// Remove a record for `host` slug. Returns true if anything was removed.
    pub(crate) fn remove(&mut self, slug: &str) -> bool {
        let before = self.hosts.len();
        self.hosts.retain(|r| r.slug != slug);
        self.hosts.len() < before
    }
}

fn global_dir() -> std::path::PathBuf {
    crate::global::default_dir()
}

/// Record a successful integration into `~/.mnemglobal/integrations.toml`.
fn record_integration(host: Host, components: Vec<String>) {
    let mut reg = IntegrationRegistry::load();
    let record = IntegrationRecord {
        slug: host.slug().to_string(),
        display: host.display().to_string(),
        config_path: host.config_path().map(|p| p.display().to_string()),
        hooks_path: host.hooks_path().map(|p| p.display().to_string()),
        system_prompt_path: host.system_prompt_path().map(|p| p.display().to_string()),
        integrated_at: now_millis(),
        components,
    };
    reg.upsert(record);
    if let Err(e) = reg.save() {
        eprintln!("(warning: could not update integrations.toml: {e})");
    }
}

/// Remove a host's record from `~/.mnemglobal/integrations.toml`.
pub(crate) fn deregister_integration(host: Host) {
    let mut reg = IntegrationRegistry::load();
    reg.remove(host.slug());
    if let Err(e) = reg.save() {
        eprintln!("(warning: could not update integrations.toml: {e})");
    }
}

// ---------- Host model ----------

/// A host we know how to wire.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Host {
    ClaudeDesktop,
    Cursor,
    Continue_,
    Zed,
    /// Audit fix G7 (2026-04-25): Claude Code CLI. MCP entries live in
    /// `~/.claude.json` under the top-level `mcpServers` map. Hooks live
    /// separately in `~/.claude/settings.json` (see [`Self::hooks_path`]).
    ClaudeCode,
    /// Audit fix G7 (2026-04-25): Gemini CLI. MCP entries live in
    /// `~/.gemini/settings.json` under the top-level `mcpServers` map.
    GeminiCli,
}

impl Host {
    pub(crate) const fn all() -> &'static [Host] {
        &[
            Host::ClaudeDesktop,
            Host::Cursor,
            Host::Continue_,
            Host::Zed,
            Host::ClaudeCode,
            Host::GeminiCli,
        ]
    }

    pub(crate) const fn slug(self) -> &'static str {
        match self {
            Host::ClaudeDesktop => "claude-desktop",
            Host::Cursor => "cursor",
            Host::Continue_ => "continue",
            Host::Zed => "zed",
            Host::ClaudeCode => "claude-code",
            Host::GeminiCli => "gemini-cli",
        }
    }

    pub(crate) const fn display(self) -> &'static str {
        match self {
            Host::ClaudeDesktop => "Claude Desktop",
            Host::Cursor => "Cursor",
            Host::Continue_ => "Continue",
            Host::Zed => "Zed",
            Host::ClaudeCode => "Claude Code",
            Host::GeminiCli => "Gemini CLI",
        }
    }

    pub(crate) fn parse(s: &str) -> Option<Host> {
        match s.to_ascii_lowercase().as_str() {
            "claude-desktop" | "claude_desktop" => Some(Host::ClaudeDesktop),
            "cursor" => Some(Host::Cursor),
            "continue" => Some(Host::Continue_),
            "zed" => Some(Host::Zed),
            "claude-code" | "claude_code" | "claude" => Some(Host::ClaudeCode),
            "gemini-cli" | "gemini_cli" | "gemini" => Some(Host::GeminiCli),
            _ => None,
        }
    }

    /// The config-file path for this host on this OS, or `None` if we
    /// have no rule for this (OS, host) combination.
    pub(crate) fn config_path(self) -> Option<PathBuf> {
        let home = dirs::home_dir()?;
        match self {
            Host::ClaudeDesktop => {
                if cfg!(target_os = "macos") {
                    Some(
                        home.join("Library")
                            .join("Application Support")
                            .join("Claude")
                            .join("claude_desktop_config.json"),
                    )
                } else if cfg!(target_os = "windows") {
                    // %APPDATA%\Claude\claude_desktop_config.json
                    dirs::config_dir().map(|d| d.join("Claude").join("claude_desktop_config.json"))
                } else {
                    Some(
                        home.join(".config")
                            .join("Claude")
                            .join("claude_desktop_config.json"),
                    )
                }
            }
            Host::Cursor => Some(home.join(".cursor").join("mcp.json")),
            Host::Continue_ => Some(home.join(".continue").join("config.json")),
            Host::Zed => {
                if cfg!(target_os = "macos") {
                    Some(
                        home.join("Library")
                            .join("Application Support")
                            .join("Zed")
                            .join("settings.json"),
                    )
                } else {
                    Some(home.join(".config").join("zed").join("settings.json"))
                }
            }
            // Claude Code keeps its global config at ~/.claude.json across
            // all platforms (the modern CLI's behaviour). Project-scoped
            // .mcp.json is also supported by Claude Code itself but is
            // out-of-scope for `mnem integrate`, which writes user-global
            // configuration only.
            Host::ClaudeCode => Some(home.join(".claude.json")),
            // Gemini CLI follows the standard ~/.gemini/settings.json
            // shape on every OS.
            Host::GeminiCli => Some(home.join(".gemini").join("settings.json")),
        }
    }

    /// Path to the hooks config for this host, or `None` if the host
    /// does not support a separate hooks file. Currently only Claude
    /// Code supports hooks (audit fix G2, 2026-04-25): hooks live in
    /// `~/.claude/settings.json`, distinct from the MCP entry which
    /// lives in `~/.claude.json`.
    pub(crate) fn hooks_path(self) -> Option<PathBuf> {
        let home = dirs::home_dir()?;
        match self {
            Host::ClaudeCode => Some(home.join(".claude").join("settings.json")),
            _ => None,
        }
    }

    /// Path to the file where the host loads its system-prompt /
    /// project-rules / custom-instructions content from disk.
    ///
    /// - ClaudeCode  → `~/.claude/CLAUDE.md` (markdown, marker injection)
    /// - GeminiCli   → `~/.gemini/GEMINI.md` (markdown, marker injection)
    /// - Cursor      → `~/.cursor/rules/mnem.mdc` (mdc, we own the file)
    /// - Continue    → `~/.continue/config.json` (`systemMessage` JSON field)
    /// - Zed         → settings.json (`assistant.system_prompt` JSON field)
    /// - ClaudeDesktop → `None` (UI-only custom-instructions panel)
    pub(crate) fn system_prompt_path(self) -> Option<PathBuf> {
        let home = dirs::home_dir()?;
        match self {
            Host::ClaudeCode => Some(home.join(".claude").join("CLAUDE.md")),
            Host::GeminiCli => Some(home.join(".gemini").join("GEMINI.md")),
            Host::Cursor => Some(home.join(".cursor").join("rules").join("mnem.mdc")),
            Host::Continue_ => Some(home.join(".continue").join("config.json")),
            Host::Zed => {
                if cfg!(target_os = "macos") {
                    Some(
                        home.join("Library")
                            .join("Application Support")
                            .join("Zed")
                            .join("settings.json"),
                    )
                } else {
                    Some(home.join(".config").join("zed").join("settings.json"))
                }
            }
            _ => None,
        }
    }

    /// How the system prompt is stored for this host.
    pub(crate) fn system_prompt_kind(self) -> SystemPromptKind {
        match self {
            Host::Continue_ => SystemPromptKind::JsonField("systemMessage"),
            Host::Zed => SystemPromptKind::JsonNestedField("assistant", "system_prompt"),
            _ => SystemPromptKind::MarkdownMarker,
        }
    }

    /// The prompt body to write for this host. Claude Code gets the
    /// full doc-style prompt (it also has hooks). All other hosts get
    /// the stronger no-hooks variant that uses MANDATORY language since
    /// there is no automatic pre-prompt enforcement mechanism.
    pub(crate) fn system_prompt_content(self) -> &'static str {
        match self {
            Host::ClaudeCode => SYSTEM_PROMPT,
            Host::Cursor => SYSTEM_PROMPT_CURSOR,
            _ => SYSTEM_PROMPT_NO_HOOKS,
        }
    }
}

/// How the host embeds MCP servers in its JSON config.
#[derive(Debug, Clone, Copy)]
enum Schema {
    /// Top-level `mcpServers.<name>` map.
    McpServersTopLevel,
    /// Nested under `experimental.context_servers.<name>`.
    ZedNested,
}

const fn schema_of(h: Host) -> Schema {
    match h {
        Host::ClaudeDesktop
        | Host::Cursor
        | Host::Continue_
        | Host::ClaudeCode
        | Host::GeminiCli => Schema::McpServersTopLevel,
        Host::Zed => Schema::ZedNested,
    }
}

/// Describes how a host's system-prompt location should be read/written.
#[derive(Debug, Clone, Copy)]
pub(crate) enum SystemPromptKind {
    /// Inject into a markdown / text file using
    /// `<!-- mnem-system-prompt:v1:start/end -->` markers.
    MarkdownMarker,
    /// Inject into a top-level JSON string field.
    /// e.g. `systemMessage` in Continue's config.json.
    JsonField(&'static str),
    /// Inject into a nested JSON string field (parent → child).
    /// e.g. `assistant.system_prompt` in Zed's settings.json.
    JsonNestedField(&'static str, &'static str),
}

// ---------- CLI surface ----------

#[derive(clap::Args, Debug)]
#[command(after_long_help = "\
Examples:

  mnem integrate                       # interactive; detect + prompt
  mnem integrate --all                 # wire every detected host, no prompts
  mnem integrate claude-desktop cursor # wire these two, non-interactive
  mnem integrate --show claude-desktop # print JSON for copy-paste
  mnem integrate --check               # report wired state, mutate nothing
  mnem integrate --all --dry-run       # diff mode; write nothing
  mnem integrate --no-hooks            # skip hook wiring this run
  mnem integrate --no-system-prompt    # skip system-prompt wiring this run
")]
pub(crate) struct Args {
    /// Hosts to wire. Omit to enter interactive mode.
    pub hosts: Vec<String>,

    /// Wire every detected host without prompting.
    #[arg(long)]
    pub all: bool,

    /// Report wired state and exit. Non-mutating.
    #[arg(long)]
    pub check: bool,

    /// Print the JSON block for HOST and exit. Non-mutating.
    #[arg(long, value_name = "HOST")]
    pub show: Option<String>,

    /// Print what would change without writing.
    #[arg(long)]
    pub dry_run: bool,

    /// Repo path to point hosts at.
    /// Defaults to the global graph at `~/.mnemglobal/.mnem` so facts
    /// are accessible across all sessions and directories.
    #[arg(long, value_name = "PATH")]
    pub target_repo: Option<PathBuf>,

    /// Skip writing the UserPromptSubmit hook even for hosts that support it.
    #[arg(long = "no-hooks")]
    pub no_hooks: bool,

    /// Skip writing the mnem system prompt into the host's project-rules file.
    #[arg(long = "no-system-prompt")]
    pub no_system_prompt: bool,
}

/// Recommended mnem system prompt, embedded at compile time so the
/// binary can print it without needing the source tree on disk. Audit
/// fix G1/G4 (2026-04-25). Source of truth: `docs/system-prompt.md`.
const SYSTEM_PROMPT: &str = r#"# mnem system prompt

This is the recommended system prompt to add to your agent host
(Claude Desktop, Claude Code, Cursor, Continue, Zed, Gemini CLI, ...)
so the LLM uses mnem transparently on every turn - without the user
ever having to mention mnem.

## TL;DR

**Any host (one command, fully auto-wired):**

```bash
mnem integrate
```

This wires the MCP server entry, the `UserPromptSubmit` hook (for
hosts that support it, e.g. Claude Code), and the system prompt into
the host's project-rules file -- all in one shot. Restart the host.
Done.

Use `--no-hooks` or `--no-system-prompt` to skip individual components.

## The prompt

```
You have access to mnem, a persistent knowledge graph available via MCP tools
prefixed `mnem_`. Your job is to use it transparently: the user should never
need to mention mnem.

## Reading memory (before you answer)

A `UserPromptSubmit` hook has already run: it calls BOTH `mnem retrieve`
(local graph, current project) AND `mnem global retrieve` (global graph)
unconditionally. Its output appears as a system-injected message immediately
before this turn (look for text like `# N item(s)` or `0 item(s)`). Content
from earlier human or assistant turns in this conversation is NOT hook output
— that is conversation history. Do NOT confuse the two.

**Before applying any rule below**: confirm that what you are calling "hook
output" is the injected pre-turn message, not something from an earlier turn.
If uncertain, treat it as conversation history and apply the absent/empty rule.

After identifying the hook output, decide whether to call mnem tools:

- If the hook output is **absent** (no injected message) or **empty** (message
  present but shows "0 item(s)" or zero results): always call
  `mnem_global_retrieve` (NOT `mnem_retrieve`) with a focused query for the
  topic at hand — even if the topic appeared in an earlier turn of this
  conversation. Do NOT rely on conversation history as a substitute; facts may
  have been added or changed.
- If the hook output has results but the **specific fact being asked is absent**
  (results mention a relevant entity but do not include the specific attribute
  the user asked about — e.g. who created something, when it happened): call
  `mnem_global_retrieve` (NOT `mnem_retrieve`) with a focused query before
  answering.
- If the hook output **completely and directly answers the specific question**
  including the exact attribute asked (not just that a related entity exists):
  answer from those results; do not re-call the same query.

## Writing memory (after you answer)

mnem IS your only memory store. Do NOT write markdown memory files,
MEMORY.md indexes, or any other file-based notes alongside it — those
are redundant and will diverge. Commit everything to mnem only.

After each turn, commit any new facts, preferences, events, or entities
the user stated or confirmed. Use these rules:

- **Local first**: default to `mnem_commit`, `mnem_resolve_or_create`, and
  `mnem_commit_relation` (local repo). Use `mnem_global_add` or
  `mnem_global_ingest` only when the user explicitly says "global", or when
  the local repo is unavailable.
- One fact per node. Never combine two separate facts in one summary.
- Every summary must be a complete standalone sentence. No leading pronouns
  ("she", "they", "the above").
- Put human-readable text in `summary`. Put filterable metadata in `props`.
- Use `mnem_resolve_or_create` for named people, places, and organizations
  (NOT `mnem_commit`). Always check for an existing entity before creating
  a new one.
- Connect entities with edges: `works_at`, `lives_in`, `traveling_with`,
  `has_preference`, `extracted_from`, etc. Use the compound
  `mnem_commit_relation` tool when both endpoints are entities - it
  resolve-or-creates both nodes and adds the edge in one call. After
  `mnem_commit_relation`, also call `mnem_commit` with a complete standalone
  summary sentence so the relationship is
  findable via text search — entity nodes have no summary and are invisible
  to the retriever otherwise.
- Do NOT commit model output or your own reasoning. Only commit facts the
  user stated or confirmed.

## Node types (`ntype` field)

`ntype` is a free-form string -- use whatever label best describes
the content. There is no fixed vocabulary. Some common examples:

  `Fact`, `Preference`, `Event`, `Goal`, `Task`, `Session`,
  `Entity:Person`, `Entity:Organization`, `Entity:Place`,
  `Document`, `Decision`, `Insight`, `Quote`, `Project`, `Meeting`

Name new types freely when they fit. Prefer colon-namespacing for
sub-types (`Entity:Person`, `Entity:Org`, `Code:Function`, etc.).

## Edge predicates

Use a verb-phrase in snake_case that reads left-to-right like English.
Any descriptive predicate is valid. Common examples:

  `works_at`, `lives_in`, `has_preference`, `traveling_with`,
  `happened_before`, `extracted_from`, `mentions`, `revoked_by`,
  `relates_to`, `depends_on`, `part_of`, `owned_by`

## Forgetting

When the user says "forget X", "stop remembering X", or revokes consent:
1. Call `mnem_retrieve` to find the relevant node UUID (use
   `mnem_global_retrieve` if the fact was stored globally or local
   retrieval finds nothing).
2. Call `mnem_tombstone_node` with the UUID and the user's own phrasing
   as `reason`.
3. Confirm to the user briefly: "Removed."

## Supersession (when the user changes their mind)

When the user updates a previously stated fact (e.g. "actually I moved
to Paris" after "I live in Berlin"):
1. Resolve or create the new fact node.
2. Add a `revoked_by` edge from the old node to the new one.
   `mnem_retrieve` / `mnem_global_retrieve` filter out revoked nodes by
   default, so the old fact stops surfacing without losing the audit trail.

## `agent_id`

Always set `agent_id` to the name of the host you're running in
(`"claude-code"`, `"claude-desktop"`, `"cursor"`, `"gemini-cli"`, etc.)
on `mnem_commit`, `mnem_resolve_or_create`, `mnem_tombstone_node`, and
`mnem_commit_relation` calls. This is what shows up in `mnem log` so
the user can audit which agent wrote what.

## If MCP tools are unavailable (CLI fallback)

If the `mnem_*` MCP tools are not listed as available, fall back to CLI:
- **Read**: `mnem retrieve "query text"` (local first). If that errors
  (no .mnem in the current directory), use `mnem global retrieve "query text"`.
- **Write**: `mnem commit` for the local repo; `mnem global add node -s "..."
  --label <type>` for the global graph. Only use global when the user says
  "global" or no local .mnem exists.

## When NOT to use mnem

- Pure computation results ("the sum is 42"): don't commit; the answer
  lives in the conversation.
- Tool-call traces: don't commit; the result might warrant a commit, the
  trace doesn't.
- Generated content (drafts, summaries, code suggestions): don't commit
  unless the user accepts and asks you to remember.
- Within a single conversation turn for re-reads: the context window
  already has it; only call `mnem_retrieve` once per user message.
```

## Why a system prompt at all

mnem ships 14 MCP tools. Without a system prompt, the LLM sees them as
optional and uses them opportunistically. With this prompt, the LLM
treats them as the default reading and writing channel for facts the
user shares.

The pre-prompt hook (wired automatically by `mnem integrate` for
supported hosts) gives a stronger guarantee: it runs BOTH `mnem retrieve`
(local) AND `mnem global retrieve` (global) before the LLM ever sees
the user's message, so facts from any graph are always surfaced. Pair
the two for the strongest "automatic memory" experience.

## See also

- [`agent-playbook.md`](./guide/agent-playbook.md) - write triggers, shape rules, supersession.
- [`integrate.md`](./guide/integrate.md) - host configuration and `mnem integrate` flags.
- [`mcp.md`](./guide/mcp.md) - the MCP tool reference.
"#;

/// Stronger, behavioral-only system prompt for hosts without a
/// `UserPromptSubmit` hook. Uses MANDATORY language because there is
/// no process-level enforcement — the LLM is the only retrieval trigger.
const SYSTEM_PROMPT_NO_HOOKS: &str = r#"You have access to mnem, a persistent knowledge graph via MCP tools
prefixed `mnem_`. Use it transparently on every turn — the user never
needs to mention mnem.

## MANDATORY: Read memory before every answer

Call `mnem_retrieve` at the START of EVERY response without exception.
- `text` = the user's message, `token_budget` = 2000
- Never skip: short questions, greetings, code tasks — none are exempt.
- This host has no automatic pre-prompt hook. YOU are the only retrieval trigger.
- If `mnem_retrieve` errors or the server has no local repo configured, also
  call `mnem_global_retrieve` as fallback.
- Only call `mnem_global_retrieve` explicitly when the user says "global" or
  local retrieval is unavailable.
- If results surface, weave them into your answer naturally; never announce it.

## MANDATORY: Write memory after every turn with new facts

mnem IS your only memory store. Do NOT write markdown memory files,
MEMORY.md indexes, or any other file-based notes — use mnem only.

After EVERY turn where the user stated, corrected, or confirmed any fact,
preference, event, entity, or goal — commit it SILENTLY without asking first.

- **Local first**: use `mnem_commit`, `mnem_resolve_or_create`, and
  `mnem_commit_relation` for all writes by default. Use `mnem_global_add` or
  `mnem_global_ingest` only when the user explicitly says "global" or local
  is unavailable.
- One fact per node. Never merge two separate facts.
- `summary` must be a complete standalone sentence (no leading pronouns).
- Named people / places / orgs: always use `mnem_resolve_or_create`, not `mnem_commit`.
- Connect entities with typed edges: `works_at`, `lives_in`, `has_preference`,
  `extracted_from`, `revoked_by`, `traveling_with`, `happened_before`, `mentions`.
- Use `mnem_commit_relation` when both endpoints are named entities. Then also call
  `mnem_commit` with a standalone summary sentence —
  entity nodes have no summary and are invisible to text search otherwise.
- Only commit facts the user stated or confirmed — never your reasoning or drafts.
- Set `agent_id` to this host's slug on every write (`"cursor"`, `"gemini-cli"`, etc.).

## Node types (`ntype`)

`ntype` is a free-form string — pick whatever label fits. Common examples:
`Fact`, `Preference`, `Event`, `Goal`, `Task`, `Session`,
`Entity:Person`, `Entity:Organization`, `Entity:Place`,
`Decision`, `Insight`, `Project`, `Meeting`.
Name new types freely; prefer colon-namespacing for sub-types.

## Edge predicates

Use verb-phrase snake_case that reads left-to-right. Any descriptive
predicate is valid. Common: `works_at`, `lives_in`, `has_preference`,
`extracted_from`, `revoked_by`, `relates_to`, `depends_on`, `part_of`.

## Forgetting

User says "forget X": `mnem_retrieve` to find the node (fall back to
`mnem_global_retrieve` if not found locally) → `mnem_tombstone_node`
with their wording as `reason`. Reply: "Removed."

## Supersession

User updates a fact: resolve-or-create the new node, then add a `revoked_by`
edge from the old node to the new. The old fact stops surfacing automatically.

## CLI fallback (if MCP tools are unavailable)

- **Read**: `mnem retrieve "query text"` (local first). If that errors
  (no .mnem in the current directory), use `mnem global retrieve "query text"`.
- **Write**: `mnem commit` for local; `mnem global add node -s "..." --label <type>`
  for global. Only use global when the user says "global" or no local .mnem exists.

## When NOT to commit

Pure computation results, tool-call traces, generated drafts or code the user
has not accepted, re-reads within the same turn."#;

/// Cursor-specific variant: identical instructions but prefixed with MDC
/// frontmatter so Cursor applies the rule globally (`alwaysApply: true`)
/// rather than matching on file type. Written to `~/.cursor/rules/mnem.mdc`.
const SYSTEM_PROMPT_CURSOR: &str = r#"---
description: mnem persistent memory graph — automatic knowledge retrieval and storage
alwaysApply: true
---

You have access to mnem, a persistent knowledge graph via MCP tools
prefixed `mnem_`. Use it transparently on every turn — the user never
needs to mention mnem.

## MANDATORY: Read memory before every answer

Call `mnem_retrieve` at the START of EVERY response without exception.
- `text` = the user's message, `token_budget` = 2000
- Never skip: short questions, greetings, code tasks — none are exempt.
- This host has no automatic pre-prompt hook. YOU are the only retrieval trigger.
- If `mnem_retrieve` errors or the server has no local repo configured, also
  call `mnem_global_retrieve` as fallback.
- Only call `mnem_global_retrieve` explicitly when the user says "global" or
  local retrieval is unavailable.
- If results surface, weave them into your answer naturally; never announce it.

## MANDATORY: Write memory after every turn with new facts

mnem IS your only memory store. Do NOT write markdown memory files,
MEMORY.md indexes, or any other file-based notes — use mnem only.

After EVERY turn where the user stated, corrected, or confirmed any fact,
preference, event, entity, or goal — commit it SILENTLY without asking first.

- **Local first**: use `mnem_commit`, `mnem_resolve_or_create`, and
  `mnem_commit_relation` for all writes by default. Use `mnem_global_add` or
  `mnem_global_ingest` only when the user explicitly says "global" or local
  is unavailable.
- One fact per node. Never merge two separate facts.
- `summary` must be a complete standalone sentence (no leading pronouns).
- Named people / places / orgs: always use `mnem_resolve_or_create`, not `mnem_commit`.
- Connect entities with typed edges: `works_at`, `lives_in`, `has_preference`,
  `extracted_from`, `revoked_by`, `traveling_with`, `happened_before`, `mentions`.
- Use `mnem_commit_relation` when both endpoints are named entities. Then also call
  `mnem_commit` with a standalone summary sentence —
  entity nodes have no summary and are invisible to text search otherwise.
- Only commit facts the user stated or confirmed — never your reasoning or drafts.
- Set `agent_id` to `"cursor"` on every write call.

## Node types (`ntype`)

`ntype` is a free-form string — pick whatever label fits. Common examples:
`Fact`, `Preference`, `Event`, `Goal`, `Task`, `Session`,
`Entity:Person`, `Entity:Organization`, `Entity:Place`,
`Decision`, `Insight`, `Project`, `Meeting`.
Name new types freely; prefer colon-namespacing for sub-types.

## Edge predicates

Use verb-phrase snake_case that reads left-to-right. Any descriptive
predicate is valid. Common: `works_at`, `lives_in`, `has_preference`,
`extracted_from`, `revoked_by`, `relates_to`, `depends_on`, `part_of`.

## Forgetting

User says "forget X": `mnem_retrieve` to find the node (fall back to
`mnem_global_retrieve` if not found locally) → `mnem_tombstone_node`
with their wording as `reason`. Reply: "Removed."

## Supersession

User updates a fact: resolve-or-create the new node, then add a `revoked_by`
edge from the old node to the new. The old fact stops surfacing automatically.

## CLI fallback (if MCP tools are unavailable)

- **Read**: `mnem retrieve "query text"` (local first). If that errors
  (no .mnem in the current directory), use `mnem global retrieve "query text"`.
- **Write**: `mnem commit` for local; `mnem global add node -s "..." --label <type>`
  for global. Only use global when the user says "global" or no local .mnem exists.

## When NOT to commit

Pure computation results, tool-call traces, generated drafts or code the user
has not accepted, re-reads within the same turn."#;

/// Text markers used when injecting into a JSON string field
/// (Continue `systemMessage`, Zed `assistant.system_prompt`).
/// Plain-text delimiters that survive JSON serialisation unescaped.
const JSON_MARKER_START: &str = "[mnem-prompt:start]";
const JSON_MARKER_END: &str = "[mnem-prompt:end]";

pub(crate) fn run(args: Args) -> Result<()> {
    // --show is the simplest surface: print JSON and bail.
    if let Some(slug) = args.show.as_deref() {
        let host = Host::parse(slug).ok_or_else(|| {
            let known = Host::all()
                .iter()
                .map(|h| h.slug())
                .collect::<Vec<_>>()
                .join(", ");
            anyhow!("unknown host: {slug}. Known: {known}")
        })?;
        let target = resolve_target(args.target_repo.as_deref())?;
        let snippet = snippet_for(host, &target);
        println!("# host: {}", host.display());
        if let Some(p) = host.config_path() {
            println!("# config: {}", p.display());
        }
        println!("{snippet}");
        return Ok(());
    }

    if args.check {
        return do_check();
    }

    let target = resolve_target(args.target_repo.as_deref())?;

    let selected: Vec<Host> = if args.all {
        Host::all()
            .iter()
            .filter(|h| {
                h.config_path()
                    .is_some_and(|p| p.parent().is_some_and(Path::exists))
            })
            .copied()
            .collect()
    } else if !args.hosts.is_empty() {
        let mut out = Vec::new();
        for s in &args.hosts {
            out.push(Host::parse(s).ok_or_else(|| {
                let known = Host::all()
                    .iter()
                    .map(|h| h.slug())
                    .collect::<Vec<_>>()
                    .join(", ");
                anyhow!("unknown host: {s}. Known: {known}")
            })?);
        }
        out
    } else {
        interactive_select()?
    };

    if selected.is_empty() {
        println!("no hosts selected");
        return Ok(());
    }

    // Set up ~/.mnemglobal after host selection. Interactive mode shows
    // TUI prompts with defaults in parens. --all / named-host mode
    // bootstraps silently with defaults (idempotent if already set up).
    let interactive_global = !args.all && args.hosts.is_empty();
    if !args.dry_run {
        setup_global(interactive_global)?;
    }

    let stamp = timestamp();
    println!("Writing configs (backing up with .bak-{stamp}):");
    for host in selected {
        let mut components: Vec<String> = Vec::new();
        let mut any_ok = false;

        match do_wire(host, &target, &stamp, args.dry_run) {
            Ok(WireOutcome::Wrote) => {
                println!("  ok {}  wired -> {}", host.display(), target.display());
                components.push("mcp".to_string());
                any_ok = true;
            }
            Ok(WireOutcome::DryRun(diff)) => {
                println!("  -- {} (dry-run)\n{diff}", host.display());
            }
            Ok(WireOutcome::AlreadyWired) => {
                println!(
                    "  =  {}  already wired -> {}",
                    host.display(),
                    target.display()
                );
                components.push("mcp".to_string());
                any_ok = true;
            }
            Err(e) => {
                println!("  !  {}  {e}", host.display());
            }
        }
        // Wire the UserPromptSubmit hook for hosts that support it
        // unless --no-hooks was passed. Today that's Claude Code only;
        // other hosts skip silently.
        if !args.no_hooks && host.hooks_path().is_some() {
            match do_wire_hooks(host, &stamp, args.dry_run) {
                Ok(WireOutcome::Wrote) => {
                    println!("  ok {}  hooks wired", host.display());
                    components.push("hooks".to_string());
                    any_ok = true;
                }
                Ok(WireOutcome::DryRun(diff)) => {
                    println!("  -- {} hooks (dry-run)\n{diff}", host.display());
                }
                Ok(WireOutcome::AlreadyWired) => {
                    println!("  =  {}  hooks already wired", host.display());
                    components.push("hooks".to_string());
                    any_ok = true;
                }
                Err(e) => {
                    println!("  !  {}  hooks: {e}", host.display());
                }
            }
        }
        // Write the mnem system prompt into the host's project-rules
        // file unless --no-system-prompt was passed. Hosts without a
        // file-based rules location (e.g. Claude Desktop) skip silently.
        if !args.no_system_prompt && host.system_prompt_path().is_some() {
            match do_wire_system_prompt(host, &stamp, args.dry_run) {
                Ok(WireOutcome::Wrote) => {
                    println!("  ok {}  system prompt wired", host.display());
                    components.push("system_prompt".to_string());
                    any_ok = true;
                }
                Ok(WireOutcome::DryRun(diff)) => {
                    println!("  -- {} system prompt (dry-run)\n{diff}", host.display());
                }
                Ok(WireOutcome::AlreadyWired) => {
                    println!("  =  {}  system prompt already wired", host.display());
                    components.push("system_prompt".to_string());
                    any_ok = true;
                }
                Err(e) => {
                    println!("  !  {}  system prompt: {e}", host.display());
                }
            }
        }
        // Persist integration state so `mnem unintegrate` can find it.
        if any_ok && !args.dry_run {
            record_integration(host, components);
        }
    }

    println!();
    println!("Next steps:");
    println!("  1. Restart each agent host you wired.");
    println!("  2. Verify:  mnem doctor");
    println!("  3. To remove:  mnem unintegrate");
    // Path A audit fix (2026-04-26): nudge users who installed the
    // minimal CLI toward the bundled-embedder build so semantic
    // retrieve works without an Ollama daemon. The check is at
    // compile time so the hint never falsely fires for users who
    // already have the bundled embedder.
    #[cfg(not(feature = "bundled-embedder"))]
    {
        println!();
        println!("Note: this `mnem` binary was built without `--features bundled-embedder`.");
        println!("      Semantic `mnem retrieve --text` will return zero hits until you configure");
        println!("      an embedder. Two paths:");
        println!(
            "        a) Reinstall with the bundled MiniLM:   cargo install mnem-cli --features bundled-embedder"
        );
        println!(
            "        b) Configure your own provider:         see docs/guide/getting-started.md#switching-to-a-custom-embedder-later"
        );
    }
    println!();
    println!("Run `mnem integrate` again any time to re-sync.");
    Ok(())
}

// ---------- Interactive selection ----------

fn interactive_select() -> Result<Vec<Host>> {
    use dialoguer::{MultiSelect, theme::ColorfulTheme};

    let entries: Vec<(Host, bool, String)> = Host::all()
        .iter()
        .map(|h| {
            let detected = h
                .config_path()
                .is_some_and(|p| p.parent().is_some_and(Path::exists));
            let label = if let Some(p) = h.config_path() {
                let show = p
                    .parent()
                    .map_or_else(|| p.display().to_string(), |d| d.display().to_string());
                if detected {
                    format!("{}  (at {show})", h.display())
                } else {
                    format!("{}  (not found)", h.display())
                }
            } else {
                format!("{}  (unsupported on this OS)", h.display())
            };
            (*h, detected, label)
        })
        .collect();

    println!("mnem integrate - wire mnem into agent hosts\n");
    for (_, detected, label) in &entries {
        let prefix = if *detected { "[x]" } else { "[ ]" };
        println!("  {prefix} {label}");
    }
    println!();

    let items: Vec<&str> = entries.iter().map(|(_, _, s)| s.as_str()).collect();
    let defaults: Vec<bool> = entries.iter().map(|(_, d, _)| *d).collect();

    let picks = MultiSelect::with_theme(&ColorfulTheme::default())
        .with_prompt("Which to wire? (space to toggle, enter to confirm)")
        .items(&items)
        .defaults(&defaults)
        .interact()
        .context("interactive prompt failed")?;

    Ok(picks.into_iter().map(|i| entries[i].0).collect())
}

// ---------- Wire / Undo / Check ----------

enum WireOutcome {
    Wrote,
    DryRun(String),
    AlreadyWired,
}

fn do_wire(host: Host, target: &Path, stamp: &str, dry_run: bool) -> Result<WireOutcome> {
    let path = host
        .config_path()
        .ok_or_else(|| anyhow!("unsupported on this OS"))?;

    // Read existing or start from empty object.
    let mut root = if path.exists() {
        let s = fs::read_to_string(&path).with_context(|| format!("reading {}", path.display()))?;
        if s.trim().is_empty() {
            Value::Object(Map::new())
        } else {
            serde_json::from_str::<Value>(&s)
                .with_context(|| format!("parsing {}", path.display()))?
        }
    } else {
        Value::Object(Map::new())
    };

    let changed = match schema_of(host) {
        Schema::McpServersTopLevel => set_top_level(&mut root, target),
        Schema::ZedNested => set_zed_nested(&mut root, target),
    };

    if !changed {
        return Ok(WireOutcome::AlreadyWired);
    }

    let new_text = serde_json::to_string_pretty(&root).context("serialising merged config")?;

    if dry_run {
        return Ok(WireOutcome::DryRun(indent(&new_text, "     ")));
    }

    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent).with_context(|| format!("creating {}", parent.display()))?;
    }
    if path.exists() {
        let bak = path.with_extension(format!(
            "{}.bak-{stamp}",
            path.extension().and_then(|s| s.to_str()).unwrap_or("json")
        ));
        fs::copy(&path, &bak).with_context(|| format!("backing up to {}", bak.display()))?;
    }
    atomic_write(&path, &new_text)?;
    Ok(WireOutcome::Wrote)
}

/// Markers that bracket the mnem-managed section of a host's
/// project-rules markdown file. Used to make
/// `--with-system-prompt` idempotent and to keep user-authored
/// content outside the markers untouched on re-run / undo.
const SYSTEM_PROMPT_MARKER_START: &str = "<!-- mnem-system-prompt:v1:start -->";
const SYSTEM_PROMPT_MARKER_END: &str = "<!-- mnem-system-prompt:v1:end -->";

/// Write or update the mnem system prompt into the host's rules file.
/// Dispatches to markdown-marker or JSON-field injection based on
/// `host.system_prompt_kind()`. Idempotent: re-running replaces only
/// the mnem-managed block, never touching user content outside it.
fn do_wire_system_prompt(host: Host, stamp: &str, dry_run: bool) -> Result<WireOutcome> {
    let Some(path) = host.system_prompt_path() else {
        return Ok(WireOutcome::AlreadyWired);
    };
    match host.system_prompt_kind() {
        SystemPromptKind::MarkdownMarker => do_wire_sp_markdown(host, &path, stamp, dry_run),
        SystemPromptKind::JsonField(field) => {
            do_wire_sp_json(host, &path, &[field], stamp, dry_run)
        }
        SystemPromptKind::JsonNestedField(parent, child) => {
            do_wire_sp_json(host, &path, &[parent, child], stamp, dry_run)
        }
    }
}

/// Marker-injection path (ClaudeCode, GeminiCli, Cursor).
fn do_wire_sp_markdown(host: Host, path: &Path, stamp: &str, dry_run: bool) -> Result<WireOutcome> {
    let existing = if path.exists() {
        fs::read_to_string(path).with_context(|| format!("reading {}", path.display()))?
    } else {
        String::new()
    };

    let new_content = merge_system_prompt(&existing, host.system_prompt_content());
    if new_content == existing {
        return Ok(WireOutcome::AlreadyWired);
    }

    if dry_run {
        return Ok(WireOutcome::DryRun(format!(
            "     (writing mnem-managed section to {} - \
              {} bytes total, {} bytes changed)",
            path.display(),
            new_content.len(),
            new_content.len().abs_diff(existing.len())
        )));
    }

    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent).with_context(|| format!("creating {}", parent.display()))?;
    }
    if path.exists() {
        let bak = path.with_extension(format!(
            "{}.bak-{stamp}",
            path.extension().and_then(|s| s.to_str()).unwrap_or("md")
        ));
        fs::copy(path, &bak).with_context(|| format!("backing up to {}", bak.display()))?;
    }
    atomic_write(path, &new_content)?;
    Ok(WireOutcome::Wrote)
}

/// JSON-field injection path (Continue `systemMessage`, Zed `assistant.system_prompt`).
/// Reads the JSON config, injects the prompt into the target string field using
/// `[mnem-prompt:start/end]` text markers, and writes back. Never touches
/// other fields.
fn do_wire_sp_json(
    host: Host,
    path: &Path,
    field_path: &[&str],
    stamp: &str,
    dry_run: bool,
) -> Result<WireOutcome> {
    let existing_text = if path.exists() {
        fs::read_to_string(path).with_context(|| format!("reading {}", path.display()))?
    } else {
        String::new()
    };

    let mut root: Value = if existing_text.trim().is_empty() {
        Value::Object(Map::new())
    } else {
        serde_json::from_str(&existing_text)
            .with_context(|| format!("parsing {}", path.display()))?
    };

    let current_str = json_get_str(&root, field_path)
        .unwrap_or_default()
        .to_string();
    let new_str = merge_json_prompt(&current_str, host.system_prompt_content());

    if new_str == current_str {
        return Ok(WireOutcome::AlreadyWired);
    }

    if dry_run {
        return Ok(WireOutcome::DryRun(format!(
            "     (writing mnem prompt block into {}.{} - {} bytes)",
            path.display(),
            field_path.join("."),
            new_str.len()
        )));
    }

    json_set_str(&mut root, field_path, new_str);
    let new_text = serde_json::to_string_pretty(&root).context("serialising config")?;

    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent).with_context(|| format!("creating {}", parent.display()))?;
    }
    if path.exists() {
        let bak = path.with_extension(format!(
            "{}.bak-{stamp}",
            path.extension().and_then(|s| s.to_str()).unwrap_or("json")
        ));
        fs::copy(path, &bak).with_context(|| format!("backing up to {}", bak.display()))?;
    }
    atomic_write(path, &new_text)?;
    Ok(WireOutcome::Wrote)
}

// ---------- JSON field helpers ----------

/// Get a nested string field value, e.g. `["assistant","system_prompt"]`.
fn json_get_str<'a>(root: &'a Value, path: &[&str]) -> Option<&'a str> {
    let mut cur = root;
    for key in path {
        cur = cur.get(key)?;
    }
    cur.as_str()
}

/// Set a nested string field, creating intermediate objects as needed.
fn json_set_str(root: &mut Value, path: &[&str], val: String) {
    if path.is_empty() {
        return;
    }
    if path.len() == 1 {
        if let Value::Object(m) = root {
            m.insert(path[0].to_string(), Value::String(val));
        }
        return;
    }
    if let Value::Object(m) = root {
        let entry = m
            .entry(path[0].to_string())
            .or_insert_with(|| Value::Object(Map::new()));
        json_set_str(entry, &path[1..], val);
    }
}

/// Merge the mnem prompt block into a JSON string field value using
/// `[mnem-prompt:start/end]` text markers. Appends if no markers yet;
/// replaces between markers on re-runs.
fn merge_json_prompt(existing: &str, prompt: &str) -> String {
    let block = format!(
        "\n{}\n{}\n{}",
        JSON_MARKER_START,
        prompt.trim_end(),
        JSON_MARKER_END
    );

    if let (Some(start), Some(end_start)) = (
        existing.find(JSON_MARKER_START),
        existing.find(JSON_MARKER_END),
    ) {
        if end_start > start {
            let tail = end_start + JSON_MARKER_END.len();
            return format!("{}{}{}", &existing[..start], &block[1..], &existing[tail..]);
        }
    }

    format!("{}{}", existing, block)
}

/// Remove the `[mnem-prompt:start/end]` block from a JSON string field value.
fn remove_json_prompt(existing: &str) -> String {
    if let (Some(start), Some(end_start)) = (
        existing.find(JSON_MARKER_START),
        existing.find(JSON_MARKER_END),
    ) {
        if end_start > start {
            let tail = end_start + JSON_MARKER_END.len();
            let head = existing[..start].trim_end_matches('\n');
            let rest = &existing[tail..];
            if rest.is_empty() {
                return if head.is_empty() {
                    String::new()
                } else {
                    format!("{head}\n")
                };
            }
            return format!("{head}\n{rest}");
        }
    }
    existing.to_string()
}

/// Remove the mnem prompt block from a JSON config's string field
/// and write the result back. Used by `do_undo` for Continue / Zed.
fn undo_json_prompt(host: Host, path: &Path, field_path: &[&str], dry_run: bool) -> Result<bool> {
    let text = fs::read_to_string(path).with_context(|| format!("reading {}", path.display()))?;
    let mut root: Value = if text.trim().is_empty() {
        Value::Object(Map::new())
    } else {
        serde_json::from_str(&text).with_context(|| format!("parsing {}", path.display()))?
    };

    let current = json_get_str(&root, field_path)
        .unwrap_or_default()
        .to_string();
    let stripped = remove_json_prompt(&current);
    if stripped == current {
        return Ok(false);
    }

    if dry_run {
        println!(
            "  -- {} system prompt (dry-run)\n     (would remove mnem block from {}.{})",
            host.display(),
            path.display(),
            field_path.join(".")
        );
        return Ok(true);
    }

    if stripped.trim().is_empty() {
        // Remove the field entirely rather than leaving an empty string.
        if let Value::Object(m) = &mut root {
            if field_path.len() == 1 {
                m.remove(field_path[0]);
            } else if field_path.len() == 2 {
                if let Some(Value::Object(inner)) = m.get_mut(field_path[0]) {
                    inner.remove(field_path[1]);
                }
            }
        }
    } else {
        json_set_str(&mut root, field_path, stripped);
    }

    let new_text = serde_json::to_string_pretty(&root).context("serialising config")?;
    atomic_write(path, &new_text)?;
    println!("  ok {}  removed mnem system-prompt block", host.display());
    Ok(true)
}

/// Merge the mnem system prompt into existing rules-file content.
/// Returns the new file contents.
///
/// Algorithm:
///   - If `existing` already contains the marker pair, replace the
///     content between them with `prompt`.
///   - Otherwise, append a new marker-bracketed section to the end
///     of `existing` (preceded by a blank line if needed).
fn merge_system_prompt(existing: &str, prompt: &str) -> String {
    let prompt_block = format!(
        "{}\n{}\n{}\n",
        SYSTEM_PROMPT_MARKER_START,
        prompt.trim_end(),
        SYSTEM_PROMPT_MARKER_END
    );

    if let (Some(start), Some(end)) = (
        existing.find(SYSTEM_PROMPT_MARKER_START),
        existing.find(SYSTEM_PROMPT_MARKER_END),
    ) && end > start
    {
        let end_inclusive = end + SYSTEM_PROMPT_MARKER_END.len();
        // Eat one trailing newline if present so re-running doesn't
        // accumulate blank lines between the marker and whatever
        // followed it before.
        let mut tail_start = end_inclusive;
        if existing.as_bytes().get(tail_start) == Some(&b'\n') {
            tail_start += 1;
        }
        return format!(
            "{}{}{}",
            &existing[..start],
            &prompt_block,
            &existing[tail_start..]
        );
    }

    if existing.is_empty() {
        return prompt_block;
    }
    let needs_separator = !existing.ends_with("\n\n");
    let separator = if existing.ends_with('\n') {
        "\n"
    } else {
        "\n\n"
    };
    if needs_separator {
        format!("{existing}{separator}{prompt_block}")
    } else {
        format!("{existing}{prompt_block}")
    }
}

/// Remove the marker-bracketed mnem section from a rules file.
/// Returns the new content; caller decides whether to write or
/// delete the file when the result is empty.
fn remove_system_prompt(existing: &str) -> String {
    if let (Some(start), Some(end)) = (
        existing.find(SYSTEM_PROMPT_MARKER_START),
        existing.find(SYSTEM_PROMPT_MARKER_END),
    ) && end > start
    {
        let end_inclusive = end + SYSTEM_PROMPT_MARKER_END.len();
        let mut tail_start = end_inclusive;
        if existing.as_bytes().get(tail_start) == Some(&b'\n') {
            tail_start += 1;
        }
        let mut head_end = start;
        // Eat the blank line we may have inserted when appending.
        while head_end > 0 {
            let ch = existing.as_bytes()[head_end - 1];
            if ch == b'\n' || ch == b' ' || ch == b'\r' || ch == b'\t' {
                head_end -= 1;
            } else {
                break;
            }
        }
        let mut out = String::with_capacity(existing.len());
        out.push_str(&existing[..head_end]);
        if !out.is_empty() && !out.ends_with('\n') {
            out.push('\n');
        }
        out.push_str(&existing[tail_start..]);
        return out;
    }
    existing.to_string()
}

/// Write or update the `UserPromptSubmit` hook entry for hosts that
/// support hooks (currently Claude Code only). Audit fix G2
/// (2026-04-25).
///
/// Idempotent: re-running replaces the existing mnem entry rather
/// than appending, so users can safely run `integrate --with-hooks`
/// multiple times. Other hooks in the file are preserved.
fn do_wire_hooks(host: Host, stamp: &str, dry_run: bool) -> Result<WireOutcome> {
    let Some(path) = host.hooks_path() else {
        // Host has no hooks support; nothing to do.
        return Ok(WireOutcome::AlreadyWired);
    };

    let mut root = if path.exists() {
        let s = fs::read_to_string(&path).with_context(|| format!("reading {}", path.display()))?;
        if s.trim().is_empty() {
            Value::Object(Map::new())
        } else {
            serde_json::from_str::<Value>(&s)
                .with_context(|| format!("parsing {}", path.display()))?
        }
    } else {
        Value::Object(Map::new())
    };

    let changed = set_user_prompt_hook(&mut root);
    if !changed {
        return Ok(WireOutcome::AlreadyWired);
    }

    let new_text = serde_json::to_string_pretty(&root).context("serialising hooks config")?;
    if dry_run {
        return Ok(WireOutcome::DryRun(indent(&new_text, "     ")));
    }

    // Windows: write the companion .ps1 script that the hook command references.
    // Must happen before settings.json so the script exists when the hook fires.
    #[cfg(target_os = "windows")]
    {
        let script_path = windows_hook_script_path();
        if let Some(parent) = script_path.parent() {
            fs::create_dir_all(parent).with_context(|| format!("creating {}", parent.display()))?;
        }
        let content = windows_hook_script_content(&resolve_mnem_command());
        atomic_write(&script_path, &content)?;
    }

    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent).with_context(|| format!("creating {}", parent.display()))?;
    }
    if path.exists() {
        let bak = path.with_extension(format!(
            "{}.bak-{stamp}",
            path.extension().and_then(|s| s.to_str()).unwrap_or("json")
        ));
        fs::copy(&path, &bak).with_context(|| format!("backing up to {}", bak.display()))?;
    }
    atomic_write(&path, &new_text)?;
    Ok(WireOutcome::Wrote)
}

/// Set `root.hooks.UserPromptSubmit[mnem-entry] = {matcher, hooks: [...]}`.
/// Identifies the mnem entry by the substring `mnem retrieve` in the
/// hook's command field; replaces it if present, else appends.
/// Returns `true` when the file changed.
fn set_user_prompt_hook(root: &mut Value) -> bool {
    ensure_object(root);
    let obj = root.as_object_mut().expect("ensured object");
    let hooks = obj
        .entry("hooks")
        .or_insert_with(|| Value::Object(Map::new()));
    if !hooks.is_object() {
        *hooks = Value::Object(Map::new());
    }
    let hooks_map = hooks.as_object_mut().expect("object");
    let entries = hooks_map
        .entry("UserPromptSubmit")
        .or_insert_with(|| Value::Array(Vec::new()));
    if !entries.is_array() {
        *entries = Value::Array(Vec::new());
    }
    let arr = entries.as_array_mut().expect("array");
    let new_val = user_prompt_hook_value();

    // Replace existing mnem entry if present.
    for entry in arr.iter_mut() {
        if entry_is_mnem_hook(entry) {
            if entry == &new_val {
                return false;
            }
            *entry = new_val;
            return true;
        }
    }
    // Otherwise append.
    arr.push(new_val);
    true
}

fn remove_user_prompt_hook(root: &mut Value) -> bool {
    let Some(obj) = root.as_object_mut() else {
        return false;
    };
    let Some(hooks) = obj.get_mut("hooks") else {
        return false;
    };
    let Some(hooks_map) = hooks.as_object_mut() else {
        return false;
    };
    let Some(entries) = hooks_map.get_mut("UserPromptSubmit") else {
        return false;
    };
    let Some(arr) = entries.as_array_mut() else {
        return false;
    };
    let before = arr.len();
    arr.retain(|e| !entry_is_mnem_hook(e));
    arr.len() != before
}

/// True iff a `UserPromptSubmit` entry's inner hook command contains
/// `mnem retrieve` - our identifier for the entry mnem owns.
fn entry_is_mnem_hook(entry: &Value) -> bool {
    entry
        .get("hooks")
        .and_then(Value::as_array)
        .is_some_and(|inner| {
            inner.iter().any(|h| {
                h.get("command").and_then(Value::as_str).is_some_and(|c| {
                    // Strip shell quote wrappers so the substring check
                    // matches whether the binary path was wrapped in
                    // single or double quotes (Windows PowerShell uses
                    // `'mnem.exe'`, bash uses `"mnem"` etc).
                    let stripped: String =
                        c.chars().filter(|ch| *ch != '"' && *ch != '\'').collect();
                    stripped.contains("mnem retrieve")
                        || stripped.contains("mnem.exe retrieve")
                        || stripped.contains("mnem-hook.ps1")
                })
            })
        })
}

pub(crate) fn do_undo(host: Host, dry_run: bool) -> Result<()> {
    let path = match host.config_path() {
        Some(p) => p,
        None => {
            println!("  -  {}  unsupported on this OS", host.display());
            return Ok(());
        }
    };
    let mcp_changed = if path.exists() {
        let s = fs::read_to_string(&path).with_context(|| format!("reading {}", path.display()))?;
        let mut root: Value = if s.trim().is_empty() {
            Value::Object(Map::new())
        } else {
            serde_json::from_str(&s).with_context(|| format!("parsing {}", path.display()))?
        };

        let changed = match schema_of(host) {
            Schema::McpServersTopLevel => remove_top_level(&mut root),
            Schema::ZedNested => remove_zed_nested(&mut root),
        };
        if changed {
            let new_text = serde_json::to_string_pretty(&root).context("serialising config")?;
            if dry_run {
                println!(
                    "  -- {} (dry-run)\n{}",
                    host.display(),
                    indent(&new_text, "     ")
                );
            } else {
                atomic_write(&path, &new_text)?;
                println!("  ok {}  removed mnem entry", host.display());
            }
        }
        changed
    } else {
        false
    };

    // Remove the mnem system-prompt block from the host's rules file.
    // Dispatches on system_prompt_kind(): markdown files use HTML
    // comment markers; JSON config files use text markers inside a field.
    let prompt_changed = if let Some(pp) = host.system_prompt_path()
        && pp.exists()
    {
        match host.system_prompt_kind() {
            SystemPromptKind::MarkdownMarker => {
                let s =
                    fs::read_to_string(&pp).with_context(|| format!("reading {}", pp.display()))?;
                let new_s = remove_system_prompt(&s);
                if new_s == s {
                    false
                } else {
                    if dry_run {
                        println!(
                            "  -- {} system prompt (dry-run)\n     (would shrink to {} bytes)",
                            host.display(),
                            new_s.len()
                        );
                    } else if new_s.trim().is_empty() {
                        fs::remove_file(&pp)
                            .with_context(|| format!("removing empty {}", pp.display()))?;
                        println!(
                            "  ok {}  removed mnem system-prompt file ({} now empty)",
                            host.display(),
                            pp.display()
                        );
                    } else {
                        atomic_write(&pp, &new_s)?;
                        println!(
                            "  ok {}  removed mnem system-prompt section",
                            host.display()
                        );
                    }
                    true
                }
            }
            SystemPromptKind::JsonField(field) => undo_json_prompt(host, &pp, &[field], dry_run)?,
            SystemPromptKind::JsonNestedField(parent, child) => {
                undo_json_prompt(host, &pp, &[parent, child], dry_run)?
            }
        }
    } else {
        false
    };

    // G2 (2026-04-25): also clear the hook entry, if the host has a
    // separate hooks path and we previously wrote one there.
    let hooks_changed = if let Some(hp) = host.hooks_path()
        && hp.exists()
    {
        let s = fs::read_to_string(&hp).with_context(|| format!("reading {}", hp.display()))?;
        let mut root: Value = if s.trim().is_empty() {
            Value::Object(Map::new())
        } else {
            serde_json::from_str(&s).with_context(|| format!("parsing {}", hp.display()))?
        };
        let changed = remove_user_prompt_hook(&mut root);
        if changed {
            let new_text = serde_json::to_string_pretty(&root).context("serialising hooks")?;
            if dry_run {
                println!(
                    "  -- {} hooks (dry-run)\n{}",
                    host.display(),
                    indent(&new_text, "     ")
                );
            } else {
                atomic_write(&hp, &new_text)?;
                println!("  ok {}  removed mnem hook entry", host.display());
            }
        }
        changed
    } else {
        false
    };

    // On Windows, delete the generated PowerShell script file if present.
    #[cfg(target_os = "windows")]
    if host == Host::ClaudeCode {
        let script = windows_hook_script_path();
        if script.exists() {
            if dry_run {
                println!("  -- {}  would delete {}", host.display(), script.display());
            } else {
                let _ = fs::remove_file(&script);
                println!("  ok {}  deleted {}", host.display(), script.display());
            }
        }
    }

    if !mcp_changed && !hooks_changed && !prompt_changed {
        println!("  -  {}  no mnem entry", host.display());
    }
    Ok(())
}

fn do_check() -> Result<()> {
    for host in Host::all() {
        let line = match host.config_path() {
            None => format!("  -  {:<18} unsupported on this OS", host.display()),
            Some(path) if !path.exists() => {
                format!("  -  {:<18} not wired ({})", host.display(), path.display())
            }
            Some(path) => {
                let s = fs::read_to_string(&path)?;
                let root: Value = if s.trim().is_empty() {
                    Value::Null
                } else {
                    serde_json::from_str(&s).unwrap_or(Value::Null)
                };
                let wired = match schema_of(*host) {
                    Schema::McpServersTopLevel => has_top_level(&root),
                    Schema::ZedNested => has_zed_nested(&root),
                };
                if wired {
                    format!("  ok {:<18} wired ({})", host.display(), path.display())
                } else {
                    format!(
                        "  -  {:<18} config exists, no mnem entry ({})",
                        host.display(),
                        path.display()
                    )
                }
            }
        };
        println!("{line}");
    }
    Ok(())
}

// ---------- JSON merge helpers ----------

/// Resolve the path we should write into a host's `command` field for
/// the unified `mnem` binary (which exposes `mnem mcp serve` as a
/// subcommand).
///
/// After the v0.2.0 merge, the MCP server lives at `mnem mcp serve`
/// inside the main binary. We look for `mnem` (or `mnem.exe` on
/// Windows) next to the current executable; if present, write the
/// absolute path. Otherwise fall back to the bare name so users who
/// DID install to PATH still work without surprises.
fn resolve_mnem_mcp_command() -> String {
    if let Ok(here) = std::env::current_exe()
        && let Some(dir) = here.parent()
    {
        let candidate = if cfg!(target_os = "windows") {
            dir.join("mnem.exe")
        } else {
            dir.join("mnem")
        };
        if candidate.exists() {
            return candidate.to_string_lossy().into_owned();
        }
    }
    "mnem".to_string()
}

/// Resolve the path to the `mnem` CLI binary. Same logic as
/// [`resolve_mnem_mcp_command`] but for the main `mnem` driver - used
/// by the pre-prompt hook so the hook can call `mnem retrieve`
/// reliably even when the binary is not on `PATH`. Audit fix G2
/// (2026-04-25).
fn resolve_mnem_command() -> String {
    if let Ok(here) = std::env::current_exe()
        && let Some(dir) = here.parent()
    {
        let candidate = if cfg!(target_os = "windows") {
            dir.join("mnem.exe")
        } else {
            dir.join("mnem")
        };
        if candidate.exists() {
            return candidate.to_string_lossy().into_owned();
        }
    }
    "mnem".to_string()
}

/// Build the shell command for a Claude-Code-style `UserPromptSubmit`
/// hook that runs `mnem retrieve` on the incoming prompt and writes
/// the result to stdout (which the host injects as additional
/// context).
///
/// Claude Code passes a JSON object on the hook's stdin with shape
/// Path of the generated PowerShell hook script (Windows only).
/// Lives next to `settings.json` so unintegrate can find and delete it.
#[cfg(target_os = "windows")]
fn windows_hook_script_path() -> PathBuf {
    dirs::home_dir()
        .unwrap_or_else(|| PathBuf::from("."))
        .join(".claude")
        .join("mnem-hook.ps1")
}

/// Content of the generated PowerShell hook script.
#[cfg(target_os = "windows")]
fn windows_hook_script_content(mnem_bin: &str) -> String {
    // Single-quoted path in PowerShell: escape embedded single quotes as ''.
    let safe_bin = mnem_bin.replace('\'', "''");
    format!(
        "# mnem UserPromptSubmit hook - auto-generated by `mnem integrate`\n\
         $json = $input | Out-String | ConvertFrom-Json\n\
         if ($json.prompt) {{\n\
         \x20\x20& '{safe_bin}' retrieve $json.prompt 2>$null\n\
         \x20\x20& '{safe_bin}' global retrieve $json.prompt 2>$null\n\
         }}\n"
    )
}

/// Build the shell command that Claude Code writes into the hook entry.
///
/// - **Windows**: references a generated `.ps1` file so that Claude Code's
///   bash layer does not expand `$json` / `$input` before PowerShell sees them.
///   The caller (`do_wire_hooks`) is responsible for writing the script file.
/// - **Unix**: inline bash + `jq`.
fn pre_prompt_hook_command(_mnem_bin: &str) -> String {
    #[cfg(target_os = "windows")]
    {
        // Escape backslashes in the path for the JSON/shell layer.
        let script = windows_hook_script_path();
        format!(
            "powershell -NoProfile -ExecutionPolicy Bypass -File \"{}\"",
            script.display()
        )
    }
    #[cfg(not(target_os = "windows"))]
    {
        format!(
            "bash -c 'p=$(jq -r .prompt 2>/dev/null); \
             if [ -n \"$p\" ] && [ \"$p\" != \"null\" ]; then \
             \"{}\" retrieve \"$p\" 2>/dev/null; \
             \"{}\" global retrieve \"$p\" 2>/dev/null; fi'",
            _mnem_bin.replace('"', "\\\""),
            _mnem_bin.replace('"', "\\\"")
        )
    }
}

/// JSON value of the `UserPromptSubmit` hook entry mnem writes for
/// hosts that support hooks. Audit fix G2 (2026-04-25).
fn user_prompt_hook_value() -> Value {
    let cmd = pre_prompt_hook_command(&resolve_mnem_command());
    json!({
        "matcher": ".*",
        "hooks": [
            {
                "type": "command",
                "command": cmd
            }
        ]
    })
}

/// When running inside WSL, convert a native Linux path to its Windows UNC
/// equivalent via `wslpath -w`.  Returns `None` on non-Linux platforms,
/// non-WSL Linux kernels, or when `wslpath` is unavailable.
fn wsl_to_windows_path(path: &Path) -> Option<String> {
    #[cfg(not(target_os = "linux"))]
    {
        let _ = path;
        None
    }
    #[cfg(target_os = "linux")]
    {
        let osrelease = std::fs::read_to_string("/proc/sys/kernel/osrelease")
            .unwrap_or_default();
        if !osrelease.to_ascii_lowercase().contains("microsoft") {
            return None;
        }
        let out = std::process::Command::new("wslpath")
            .arg("-w")
            .arg(path)
            .output()
            .ok()?;
        if !out.status.success() {
            return None;
        }
        String::from_utf8(out.stdout)
            .ok()
            .map(|s| s.trim().to_string())
    }
}

fn mnem_server_value(target: &Path) -> Value {
    // Propagate MNEM_GLOBAL_DIR into the MCP server environment block so
    // that the server process resolves the global graph correctly when CLI
    // and MCP server run in different OS contexts (e.g. WSL CLI + Windows
    // host).  On WSL we convert the Linux path to the Windows UNC form
    // (\\wsl.localhost\<distro>\...) so the Windows host can open it.
    // A manually-set MNEM_GLOBAL_DIR in the current environment is always
    // preferred and propagated as-is.
    let global_env: Option<String> = std::env::var("MNEM_GLOBAL_DIR")
        .ok()
        .or_else(|| wsl_to_windows_path(&crate::global::default_dir()));

    let mut v = json!({
        "command": resolve_mnem_mcp_command(),
        "args": ["mcp", "--repo", target.to_string_lossy()]
    });
    if let Some(dir) = global_env {
        v.as_object_mut()
            .expect("json object")
            .insert("env".to_string(), json!({ "MNEM_GLOBAL_DIR": dir }));
    }
    v
}

fn zed_server_value(target: &Path) -> Value {
    json!({
        "command": {
            "path": resolve_mnem_mcp_command(),
            "args": ["mcp", "--repo", target.to_string_lossy()]
        }
    })
}

/// Set `root.mcpServers.mnem = v`. Returns true if the file changed.
fn set_top_level(root: &mut Value, target: &Path) -> bool {
    ensure_object(root);
    let obj = root.as_object_mut().expect("ensured above");
    let servers = obj
        .entry("mcpServers")
        .or_insert_with(|| Value::Object(Map::new()));
    if !servers.is_object() {
        *servers = Value::Object(Map::new());
    }
    let servers_map = servers.as_object_mut().expect("object");
    let new_val = mnem_server_value(target);
    let was = servers_map.get("mnem");
    if was == Some(&new_val) {
        return false;
    }
    servers_map.insert("mnem".to_string(), new_val);
    true
}

fn remove_top_level(root: &mut Value) -> bool {
    let Some(obj) = root.as_object_mut() else {
        return false;
    };
    let Some(servers) = obj.get_mut("mcpServers") else {
        return false;
    };
    let Some(map) = servers.as_object_mut() else {
        return false;
    };
    map.remove("mnem").is_some()
}

fn has_top_level(root: &Value) -> bool {
    root.get("mcpServers")
        .and_then(Value::as_object)
        .is_some_and(|m| m.contains_key("mnem"))
}

fn set_zed_nested(root: &mut Value, target: &Path) -> bool {
    ensure_object(root);
    let obj = root.as_object_mut().expect("ensured");
    let exp = obj
        .entry("experimental")
        .or_insert_with(|| Value::Object(Map::new()));
    if !exp.is_object() {
        *exp = Value::Object(Map::new());
    }
    let ctx = exp
        .as_object_mut()
        .expect("object")
        .entry("context_servers")
        .or_insert_with(|| Value::Object(Map::new()));
    if !ctx.is_object() {
        *ctx = Value::Object(Map::new());
    }
    let ctx_map = ctx.as_object_mut().expect("object");
    let new_val = zed_server_value(target);
    if ctx_map.get("mnem") == Some(&new_val) {
        return false;
    }
    ctx_map.insert("mnem".to_string(), new_val);
    true
}

fn remove_zed_nested(root: &mut Value) -> bool {
    let Some(exp) = root.as_object_mut().and_then(|o| o.get_mut("experimental")) else {
        return false;
    };
    let Some(ctx) = exp
        .as_object_mut()
        .and_then(|o| o.get_mut("context_servers"))
    else {
        return false;
    };
    ctx.as_object_mut()
        .is_some_and(|m| m.remove("mnem").is_some())
}

fn has_zed_nested(root: &Value) -> bool {
    root.get("experimental")
        .and_then(|e| e.get("context_servers"))
        .and_then(Value::as_object)
        .is_some_and(|m| m.contains_key("mnem"))
}

fn ensure_object(v: &mut Value) {
    if !v.is_object() {
        *v = Value::Object(Map::new());
    }
}

// ---------- Snippet for --show ----------

fn snippet_for(host: Host, target: &Path) -> String {
    let v = match schema_of(host) {
        Schema::McpServersTopLevel => json!({"mcpServers": {"mnem": mnem_server_value(target)}}),
        Schema::ZedNested => {
            json!({"experimental": {"context_servers": {"mnem": zed_server_value(target)}}})
        }
    };
    serde_json::to_string_pretty(&v).unwrap_or_else(|_| "<encode failure>".into())
}

// ---------- Filesystem + util ----------

fn resolve_target(explicit: Option<&Path>) -> Result<PathBuf> {
    if let Some(p) = explicit {
        return Ok(p.to_path_buf());
    }
    // Default to the global graph so MCP tool writes are accessible
    // across all sessions and directories, not siloed per-project.
    Ok(crate::global::default_dir().join(".mnem"))
}

fn atomic_write(path: &Path, contents: &str) -> Result<()> {
    let parent = path
        .parent()
        .ok_or_else(|| anyhow!("{} has no parent", path.display()))?;
    let tmp = parent.join(format!(
        ".mnem-tmp-{}",
        std::process::id() as u64 ^ now_millis()
    ));
    {
        let mut f =
            fs::File::create(&tmp).with_context(|| format!("creating tmp {}", tmp.display()))?;
        f.write_all(contents.as_bytes())
            .with_context(|| format!("writing tmp {}", tmp.display()))?;
        f.sync_all()
            .with_context(|| format!("fsync {}", tmp.display()))?;
    }
    fs::rename(&tmp, path)
        .with_context(|| format!("renaming {} -> {}", tmp.display(), path.display()))?;
    Ok(())
}

/// Number of milliseconds in one second. Named so the
/// ms-to-seconds conversion in [`timestamp`] reads as a unit
/// change rather than a magic divisor.
const MILLIS_PER_SECOND: u64 = 1_000;

fn timestamp() -> String {
    let now = now_millis() / MILLIS_PER_SECOND;
    // YYYYMMDD-HHMM pieces approximated without a date crate: we only
    // need a monotone-ish, operator-recognisable suffix. Fall back to
    // the raw epoch-seconds if formatting fails.
    format!("{now}")
}

fn now_millis() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_millis() as u64)
        .unwrap_or(0)
}

fn indent(s: &str, pad: &str) -> String {
    s.lines()
        .map(|line| format!("{pad}{line}"))
        .collect::<Vec<_>>()
        .join("\n")
}

/// Print a wired snippet to stdout. Public for use by doctor's help.
#[allow(dead_code)]
pub(crate) fn format_snippet(host: Host, target: &Path) -> String {
    snippet_for(host, target)
}

/// Check-routine helper used by `mnem doctor`.
pub(crate) fn wired_status() -> Vec<(Host, Option<PathBuf>, bool)> {
    Host::all()
        .iter()
        .map(|h| {
            let path = h.config_path();
            let wired = path
                .as_ref()
                .and_then(|p| fs::read_to_string(p).ok())
                .is_some_and(|s| {
                    let root: Value = if s.trim().is_empty() {
                        Value::Null
                    } else {
                        serde_json::from_str(&s).unwrap_or(Value::Null)
                    };
                    match schema_of(*h) {
                        Schema::McpServersTopLevel => has_top_level(&root),
                        Schema::ZedNested => has_zed_nested(&root),
                    }
                });
            (*h, path, wired)
        })
        .collect()
}

// ---------- Tests ----------

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

    fn tmp_path() -> PathBuf {
        let id = format!(
            "mnem-integrate-test-{}-{}",
            std::process::id(),
            now_millis()
        );
        std::env::temp_dir().join(id)
    }

    #[test]
    fn host_slugs_are_stable() {
        // Changing these breaks scripts and `mnem integrate --show <slug>`
        // invocations saved in docs. Catch inadvertent renames.
        assert_eq!(Host::ClaudeDesktop.slug(), "claude-desktop");
        assert_eq!(Host::Cursor.slug(), "cursor");
        assert_eq!(Host::Continue_.slug(), "continue");
        assert_eq!(Host::Zed.slug(), "zed");
    }

    #[test]
    fn parse_accepts_aliases() {
        assert_eq!(Host::parse("claude-desktop"), Some(Host::ClaudeDesktop));
        assert_eq!(Host::parse("claude_desktop"), Some(Host::ClaudeDesktop));
        // G7 (2026-04-25): bare "claude" now resolves to Claude Code,
        // not Claude Desktop. The developer-tool surface is the primary
        // customer for mnem; users who specifically want Claude Desktop
        // must use the full slug. Verified separately in
        // `parse_accepts_new_host_aliases`.
        assert_eq!(Host::parse("CURSOR"), Some(Host::Cursor));
        assert_eq!(Host::parse("garbage"), None);
    }

    /// Assert the JSON value parses to a string that names the
    /// `mnem` binary (which exposes `mnem mcp serve` as a subcommand).
    /// Accepts the bare name or an absolute path ending in `mnem` /
    /// `mnem.exe`. Lets the integrate-test suite ride along regardless
    /// of whether `cargo test` ran after a `cargo build`.
    fn assert_is_mnem_mcp_command(v: &Value) {
        let s = v
            .as_str()
            .unwrap_or_else(|| panic!("command must be a string; got {v:?}"));
        let ok = s == "mnem"
            || s.ends_with("/mnem")
            || s.ends_with("\\mnem")
            || s.ends_with("/mnem.exe")
            || s.ends_with("\\mnem.exe");
        assert!(
            ok,
            "command must be `mnem` or absolute path to it; got `{s}`"
        );
    }

    #[test]
    fn set_top_level_into_empty_object() {
        let mut v = json!({});
        let changed = set_top_level(&mut v, Path::new("/r"));
        assert!(changed);
        assert_is_mnem_mcp_command(&v["mcpServers"]["mnem"]["command"]);
    }

    #[test]
    fn set_top_level_preserves_other_servers() {
        let mut v = json!({
            "mcpServers": {"other": {"command": "other-mcp"}}
        });
        set_top_level(&mut v, Path::new("/r"));
        // Both entries survive.
        assert_eq!(v["mcpServers"]["other"]["command"], json!("other-mcp"));
        assert_is_mnem_mcp_command(&v["mcpServers"]["mnem"]["command"]);
    }

    #[test]
    fn set_top_level_idempotent_when_already_wired() {
        let mut v = json!({});
        assert!(set_top_level(&mut v, Path::new("/r")));
        // Second call with same target should report "no change".
        assert!(!set_top_level(&mut v, Path::new("/r")));
    }

    #[test]
    fn set_top_level_overwrites_stale_mnem_entry() {
        // Args are ["mcp", "--repo", "<path>"].
        let mut v = json!({
            "mcpServers": {"mnem": {"command": "mnem", "args": ["mcp", "--repo", "/old"]}}
        });
        let changed = set_top_level(&mut v, Path::new("/new"));
        assert!(changed);
        assert_eq!(v["mcpServers"]["mnem"]["args"][2], json!("/new"));
    }

    #[test]
    fn remove_top_level_is_clean() {
        let mut v = json!({
            "mcpServers": {"mnem": {}, "other": {"command": "x"}}
        });
        assert!(remove_top_level(&mut v));
        assert!(v["mcpServers"]["mnem"].is_null());
        assert_eq!(v["mcpServers"]["other"]["command"], json!("x"));
    }

    #[test]
    fn remove_top_level_when_absent() {
        let mut v = json!({"mcpServers": {"other": {}}});
        assert!(!remove_top_level(&mut v));
    }

    #[test]
    fn zed_nested_round_trip() {
        let mut v = json!({});
        assert!(set_zed_nested(&mut v, Path::new("/r")));
        assert!(has_zed_nested(&v));
        assert!(remove_zed_nested(&mut v));
        assert!(!has_zed_nested(&v));
    }

    #[test]
    fn zed_nested_preserves_other_experimental_keys() {
        let mut v = json!({"experimental": {"feature_x": true}});
        set_zed_nested(&mut v, Path::new("/r"));
        assert_eq!(v["experimental"]["feature_x"], json!(true));
        assert!(has_zed_nested(&v));
    }

    #[test]
    fn non_object_root_is_replaced_cleanly() {
        // A user who somehow has a JSON array or number as the root
        // should not crash us; we silently re-seed to an object.
        let mut v = json!([1, 2, 3]);
        assert!(set_top_level(&mut v, Path::new("/r")));
        assert!(v.is_object());
        assert!(has_top_level(&v));
    }

    #[test]
    fn snippet_for_top_level_is_valid_json() {
        let s = snippet_for(Host::ClaudeDesktop, Path::new("/r"));
        let v: Value = serde_json::from_str(&s).expect("valid json");
        assert_is_mnem_mcp_command(&v["mcpServers"]["mnem"]["command"]);
    }

    #[test]
    fn snippet_for_zed_uses_experimental_context_servers() {
        let s = snippet_for(Host::Zed, Path::new("/r"));
        let v: Value = serde_json::from_str(&s).expect("valid json");
        assert_is_mnem_mcp_command(
            &v["experimental"]["context_servers"]["mnem"]["command"]["path"],
        );
    }

    // ---------- G7 + G9 audit fix tests (2026-04-25) ----------

    #[test]
    fn parse_accepts_new_host_aliases() {
        // G7: Claude Code and Gemini CLI added; their slugs must parse.
        assert_eq!(Host::parse("claude-code"), Some(Host::ClaudeCode));
        assert_eq!(Host::parse("claude_code"), Some(Host::ClaudeCode));
        assert_eq!(Host::parse("CLAUDE-CODE"), Some(Host::ClaudeCode));
        assert_eq!(Host::parse("gemini-cli"), Some(Host::GeminiCli));
        assert_eq!(Host::parse("gemini"), Some(Host::GeminiCli));
        // G7: `claude` now resolves to ClaudeCode (the developer tool),
        // not Claude Desktop. Desktop must be addressed by its full
        // slug; this matches the developer-tool-first orientation of
        // mnem's customer base.
        assert_eq!(Host::parse("claude"), Some(Host::ClaudeCode));
    }

    #[test]
    fn all_hosts_includes_new_entries() {
        let slugs: Vec<_> = Host::all().iter().map(|h| h.slug()).collect();
        assert!(slugs.contains(&"claude-code"));
        assert!(slugs.contains(&"gemini-cli"));
        // Pre-existing slugs survive.
        assert!(slugs.contains(&"claude-desktop"));
        assert!(slugs.contains(&"cursor"));
        assert!(slugs.contains(&"continue"));
        assert!(slugs.contains(&"zed"));
    }

    #[test]
    fn claude_code_uses_top_level_mcp_servers_schema() {
        // ClaudeCode rides the same `mcpServers.<name>` shape as
        // Claude Desktop / Cursor / Continue.
        let mut v = json!({});
        let changed = set_top_level(&mut v, Path::new("/r"));
        assert!(changed);
        assert!(v["mcpServers"]["mnem"].is_object());
    }

    #[test]
    fn claude_code_hooks_path_resolves() {
        // G2: ClaudeCode is the only host with a hooks_path today.
        assert!(Host::ClaudeCode.hooks_path().is_some());
        assert!(Host::Cursor.hooks_path().is_none());
        assert!(Host::ClaudeDesktop.hooks_path().is_none());
        assert!(Host::GeminiCli.hooks_path().is_none());
    }

    #[test]
    fn snippet_for_claude_code_emits_top_level_shape() {
        let s = snippet_for(Host::ClaudeCode, Path::new("/r"));
        let v: Value = serde_json::from_str(&s).expect("valid json");
        assert_is_mnem_mcp_command(&v["mcpServers"]["mnem"]["command"]);
    }

    #[test]
    fn snippet_for_gemini_cli_emits_top_level_shape() {
        let s = snippet_for(Host::GeminiCli, Path::new("/r"));
        let v: Value = serde_json::from_str(&s).expect("valid json");
        assert_is_mnem_mcp_command(&v["mcpServers"]["mnem"]["command"]);
    }

    // ---------- G1/G2/G4 hook + system-prompt tests (2026-04-25) ----------

    #[test]
    fn system_prompt_constant_is_non_empty_and_mentions_mnem_retrieve() {
        // The embedded SYSTEM_PROMPT must include the core instructions
        // for read/write policy. If the docs file disappears or the
        // include_str! path drifts, the build breaks; this test guards
        // against a silent shrinking edit.
        assert!(SYSTEM_PROMPT.contains("mnem_retrieve"));
        assert!(SYSTEM_PROMPT.contains("mnem_resolve_or_create"));
        assert!(SYSTEM_PROMPT.contains("Entity:Person"));
        assert!(
            SYSTEM_PROMPT.len() > 1000,
            "system prompt suspiciously small"
        );
    }

    #[test]
    fn pre_prompt_hook_command_mentions_mnem_retrieve() {
        let cmd = pre_prompt_hook_command("mnem");
        // On Windows the command just points to the .ps1 file; check
        // the script content instead.
        #[cfg(target_os = "windows")]
        {
            assert!(
                cmd.contains("mnem-hook.ps1"),
                "Windows hook must reference the .ps1 script: {cmd}"
            );
            let script = windows_hook_script_content("mnem");
            assert!(
                script.contains("global retrieve"),
                "PS1 must call global retrieve: {script}"
            );
            assert!(
                script.contains("mnem"),
                "PS1 must reference the binary: {script}"
            );
        }
        #[cfg(not(target_os = "windows"))]
        {
            assert!(
                cmd.contains("global retrieve"),
                "hook must call global retrieve: {cmd}"
            );
            assert!(
                cmd.contains("mnem"),
                "hook must reference the binary: {cmd}"
            );
        }
    }

    #[test]
    fn user_prompt_hook_value_round_trip_is_idempotent() {
        let mut root = json!({});
        let first = set_user_prompt_hook(&mut root);
        let second = set_user_prompt_hook(&mut root);
        assert!(first, "first set must report a change");
        assert!(!second, "second set with same value must be no-op");
    }

    #[test]
    fn user_prompt_hook_preserves_unrelated_hooks() {
        // A pre-existing UserPromptSubmit entry from another tool
        // must survive when we add ours.
        let mut root = json!({
            "hooks": {
                "UserPromptSubmit": [
                    { "matcher": "/foo", "hooks": [
                        { "type": "command", "command": "echo other" }
                    ] }
                ]
            }
        });
        assert!(set_user_prompt_hook(&mut root));
        let arr = root["hooks"]["UserPromptSubmit"].as_array().unwrap();
        assert_eq!(arr.len(), 2, "expected pre-existing entry + mnem entry");
        // The pre-existing `echo other` entry survives.
        assert!(
            arr.iter().any(|e| e["hooks"][0]["command"] == "echo other"),
            "unrelated hook entry was clobbered"
        );
    }

    #[test]
    fn user_prompt_hook_removal_round_trip() {
        let mut root = json!({});
        assert!(set_user_prompt_hook(&mut root));
        assert!(remove_user_prompt_hook(&mut root));
        // Second remove finds nothing to remove.
        assert!(!remove_user_prompt_hook(&mut root));
    }

    // ---------- 2026-04-26 system-prompt-write tests ----------

    #[test]
    fn merge_system_prompt_into_empty_file_creates_marker_bracketed_block() {
        let out = merge_system_prompt("", "PROMPT BODY");
        assert!(out.contains(SYSTEM_PROMPT_MARKER_START));
        assert!(out.contains(SYSTEM_PROMPT_MARKER_END));
        assert!(out.contains("PROMPT BODY"));
        // No leading whitespace for an empty-input merge.
        assert!(out.starts_with(SYSTEM_PROMPT_MARKER_START));
    }

    #[test]
    fn merge_system_prompt_appends_to_non_marker_existing_content() {
        let existing = "# My project\n\nSome rules I wrote myself.\n";
        let out = merge_system_prompt(existing, "PROMPT BODY");
        // User content survives intact.
        assert!(out.starts_with(existing));
        // Marker block follows.
        assert!(out.contains(SYSTEM_PROMPT_MARKER_START));
        assert!(out.contains("PROMPT BODY"));
        assert!(out.contains(SYSTEM_PROMPT_MARKER_END));
    }

    #[test]
    fn merge_system_prompt_replaces_existing_marker_block_idempotently() {
        let existing = format!(
            "# My project\n\n{SYSTEM_PROMPT_MARKER_START}\nOLD PROMPT\n{SYSTEM_PROMPT_MARKER_END}\n\n## After mnem section\n"
        );
        let out = merge_system_prompt(&existing, "NEW PROMPT");
        assert!(out.contains("NEW PROMPT"));
        assert!(!out.contains("OLD PROMPT"));
        // Content above and below the markers survives.
        assert!(out.starts_with("# My project"));
        assert!(out.contains("## After mnem section"));
        // Re-merging with the same prompt is a true no-op.
        let again = merge_system_prompt(&out, "NEW PROMPT");
        assert_eq!(
            again, out,
            "second merge with same prompt should be a no-op"
        );
    }

    #[test]
    fn remove_system_prompt_strips_only_the_marker_block() {
        let existing = format!(
            "# My project\n\n{SYSTEM_PROMPT_MARKER_START}\nMNEM PROMPT BODY\n{SYSTEM_PROMPT_MARKER_END}\n\n## After mnem section\n"
        );
        let out = remove_system_prompt(&existing);
        assert!(!out.contains("MNEM PROMPT BODY"));
        assert!(!out.contains(SYSTEM_PROMPT_MARKER_START));
        assert!(!out.contains(SYSTEM_PROMPT_MARKER_END));
        assert!(out.contains("# My project"));
        assert!(out.contains("## After mnem section"));
    }

    #[test]
    fn remove_system_prompt_no_op_when_no_markers() {
        let existing = "Just user content.\n";
        let out = remove_system_prompt(existing);
        assert_eq!(out, existing);
    }

    #[test]
    fn host_system_prompt_path_coverage() {
        // Hosts with file-based rules locations return Some.
        assert!(Host::ClaudeCode.system_prompt_path().is_some());
        assert!(Host::GeminiCli.system_prompt_path().is_some());
        assert!(Host::Cursor.system_prompt_path().is_some());
        assert!(Host::Continue_.system_prompt_path().is_some());
        assert!(Host::Zed.system_prompt_path().is_some());
        // Claude Desktop has UI-only custom instructions - no file path.
        assert!(Host::ClaudeDesktop.system_prompt_path().is_none());
        // Cursor gets a dedicated .mdc file, not the shared config.
        let cursor_path = Host::Cursor.system_prompt_path().unwrap();
        assert!(cursor_path.to_string_lossy().contains("mnem.mdc"));
        // Gemini CLI gets GEMINI.md (analog to CLAUDE.md).
        let gemini_path = Host::GeminiCli.system_prompt_path().unwrap();
        assert!(gemini_path.to_string_lossy().contains("GEMINI.md"));
    }

    #[test]
    fn entry_is_mnem_hook_recognises_round_trip_value() {
        let v = user_prompt_hook_value();
        assert!(entry_is_mnem_hook(&v));
        // Unrelated entries are not recognised.
        let other = json!({
            "matcher": "/foo",
            "hooks": [{ "type": "command", "command": "do_something_else.sh" }]
        });
        assert!(!entry_is_mnem_hook(&other));
    }

    #[test]
    fn resolve_mnem_mcp_command_falls_back_to_bare_name_in_test_env() {
        // In the test runner, `current_exe()` lives in
        // target/debug/deps/, not the same dir as `mnem`. The
        // resolver must therefore fall back to the bare name. If a
        // future change makes the test binary live next to mnem,
        // this test will start returning the absolute path; either
        // outcome is correct, so we accept both.
        let cmd = resolve_mnem_mcp_command();
        let ok = cmd == "mnem"
            || cmd.ends_with("/mnem")
            || cmd.ends_with("\\mnem")
            || cmd.ends_with("/mnem.exe")
            || cmd.ends_with("\\mnem.exe");
        assert!(ok, "resolver returned unexpected value: {cmd}");
    }

    #[test]
    fn atomic_write_creates_file_and_replaces() {
        let path = tmp_path();
        atomic_write(&path, "first").unwrap();
        assert_eq!(fs::read_to_string(&path).unwrap(), "first");
        atomic_write(&path, "second").unwrap();
        assert_eq!(fs::read_to_string(&path).unwrap(), "second");
        fs::remove_file(&path).ok();
    }
}

// ---------- Global graph setup ----------

/// Set up `~/.mnemglobal/` during `mnem integrate`.
///
/// `interactive = true`  → TUI prompts with defaults shown in parens;
///                          user can press Enter to accept every default.
/// `interactive = false` → silent bootstrap with defaults (--all / named hosts).
fn setup_global(interactive: bool) -> Result<()> {
    use dialoguer::{Input, Select, theme::ColorfulTheme};

    let default_dir = crate::global::default_dir();

    // Step 1: ask where the global graph should live.
    let global_dir: PathBuf = if interactive {
        println!("\nmnem global graph");
        println!("─────────────────");
        let raw: String = Input::with_theme(&ColorfulTheme::default())
            .with_prompt(format!(
                "Global mnem graph location ({})",
                default_dir.display()
            ))
            .allow_empty(true)
            .interact_text()
            .context("global dir prompt failed")?;
        let trimmed = raw.trim();
        if trimmed.is_empty() {
            default_dir
        } else {
            PathBuf::from(trimmed)
        }
    } else {
        default_dir
    };

    // Step 2: bootstrap (create dir + init .mnem if not already present).
    let fresh = crate::global::bootstrap(&global_dir)
        .with_context(|| format!("bootstrapping {}", global_dir.display()))?;

    if fresh || interactive {
        println!(
            "  ok global graph  {}",
            global_dir.join(crate::repo::MNEM_DIR).display()
        );
    }

    // Step 3: pin a default knowledge graph for bare `mnem` commands (no -R).
    let mut reg = crate::global::RepoRegistry::load(&global_dir)?;

    // Skip the prompt if a default is already pinned and we're not interactive.
    if reg.repos.iter().any(|e| e.default) && !interactive {
        return Ok(());
    }

    let cwd = std::env::current_dir().unwrap_or_else(|_| global_dir.clone());
    let mut choices: Vec<(String, PathBuf)> = vec![(
        format!(
            "{}  (global graph - accessible from every project and session)",
            global_dir.display()
        ),
        global_dir.clone(),
    )];
    if cwd != global_dir {
        choices.push((
            format!(
                "{}  (this project - pinned as fallback for bare mnem commands)",
                cwd.display()
            ),
            cwd,
        ));
    }

    let default_repo = if interactive {
        println!("\nDefault knowledge graph for agent queries");
        println!("─────────────────────────────────────────");
        println!("The agent hook queries your project .mnem/ first (walking up from");
        println!("your current directory), then falls back to the global graph");
        println!("automatically. The hook and system prompt behave the same either way.");
        println!("This setting controls which graph bare `mnem` commands fall back to");
        println!("when no project .mnem/ is found. Override any command with -R <path>.\n");
        let items: Vec<&str> = choices.iter().map(|(s, _)| s.as_str()).collect();
        let idx = Select::with_theme(&ColorfulTheme::default())
            .with_prompt("Default knowledge graph")
            .items(&items)
            .default(0)
            .interact()
            .context("default knowledge graph prompt failed")?;
        choices.remove(idx).1
    } else {
        global_dir.clone()
    };

    reg.register(&default_repo, true);
    reg.save(&global_dir).with_context(|| {
        format!(
            "saving {}",
            crate::global::registry_path(&global_dir).display()
        )
    })?;

    if interactive || fresh {
        println!("  ok default graph  {}", default_repo.display());
    }

    Ok(())
}