hanzo-mcp 1.1.23

Hanzo MCP server — a hanzo-mcp binary serving 15 hand-written tools (fs, exec, code, git, fetch, workspace, computer, browser, think, memory, plan, tasks, mode, hanzo, search) over JSON-RPC
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
/// Memory and knowledge management tool (HIP-0300)
///
/// Provides persistent memory capabilities:
/// - recall: Search memories
/// - create: Store new memories
/// - update: Update existing memories
/// - delete: Remove memories
/// - facts: Manage knowledge base facts
/// - summarize: Summarize and store information
/// - kb: Knowledge base management
/// - help: Documentation
///
/// Persistence: ~/.hanzo/memory.json (cross-runtime compatible with TypeScript)

use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::sync::RwLock;

// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------

/// Memory scope
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum MemoryScope {
    Session,
    Project,
    Global,
}

impl Default for MemoryScope {
    fn default() -> Self {
        Self::Project
    }
}

impl std::str::FromStr for MemoryScope {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self> {
        match s.to_lowercase().as_str() {
            "session" => Ok(Self::Session),
            "project" => Ok(Self::Project),
            "global" => Ok(Self::Global),
            _ => Ok(Self::Project),
        }
    }
}

impl std::fmt::Display for MemoryScope {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Session => write!(f, "session"),
            Self::Project => write!(f, "project"),
            Self::Global => write!(f, "global"),
        }
    }
}

/// Memory action types
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum MemoryAction {
    Recall,
    Create,
    Update,
    Delete,
    Manage,
    Facts,
    Summarize,
    List,
    Stats,
    Clear,
    Export,
    Import,
    Merge,
    Tag,
    Untag,
    Namespaces,
    History,
    Kb,
    Help,
}

impl Default for MemoryAction {
    fn default() -> Self {
        Self::Help
    }
}

impl std::str::FromStr for MemoryAction {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self> {
        match s.to_lowercase().as_str() {
            "recall" | "search" | "query" => Ok(Self::Recall),
            "create" | "add" | "store" => Ok(Self::Create),
            "update" | "modify" => Ok(Self::Update),
            "delete" | "remove" => Ok(Self::Delete),
            "manage" => Ok(Self::Manage),
            "facts" | "fact" => Ok(Self::Facts),
            "summarize" | "summary" => Ok(Self::Summarize),
            "list" => Ok(Self::List),
            "stats" => Ok(Self::Stats),
            "clear" => Ok(Self::Clear),
            "export" => Ok(Self::Export),
            "import" | "import_memories" => Ok(Self::Import),
            "merge" => Ok(Self::Merge),
            "tag" => Ok(Self::Tag),
            "untag" => Ok(Self::Untag),
            "namespaces" => Ok(Self::Namespaces),
            "history" => Ok(Self::History),
            "kb" => Ok(Self::Kb),
            "help" | "" => Ok(Self::Help),
            _ => Err(anyhow!("Unknown action: {}", s)),
        }
    }
}

/// A stored memory
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Memory {
    pub id: String,
    pub content: String,
    pub scope: MemoryScope,
    pub created_at: String,
    pub updated_at: String,
    pub metadata: HashMap<String, Value>,
}

/// A fact in a knowledge base
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Fact {
    pub id: String,
    pub content: String,
    pub kb_name: String,
    pub scope: MemoryScope,
    pub created_at: String,
}

/// Knowledge base
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct KnowledgeBase {
    pub name: String,
    pub description: Option<String>,
    pub scope: MemoryScope,
    pub facts: Vec<Fact>,
    pub created_at: String,
}

// ---------------------------------------------------------------------------
// Persistence format — matches TypeScript ~/.hanzo/memory.json
// ---------------------------------------------------------------------------

/// On-disk entry format (TypeScript-compatible)
#[derive(Debug, Clone, Serialize, Deserialize)]
struct PersistEntry {
    id: String,
    key: String,
    value: String,
    tags: Vec<String>,
    namespace: String,
    created: String,
    updated: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    metadata: Option<HashMap<String, Value>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    ttl: Option<String>,
}

/// On-disk fact format
#[derive(Debug, Clone, Serialize, Deserialize)]
struct PersistFact {
    id: String,
    content: String,
    kb_name: String,
    scope: String,
    created: String,
}

/// On-disk KB metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
struct PersistKb {
    name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    description: Option<String>,
    scope: String,
    created: String,
}

/// On-disk store format (TypeScript-compatible)
#[derive(Debug, Clone, Serialize, Deserialize)]
struct PersistStore {
    entries: Vec<PersistEntry>,
    #[serde(rename = "lastId")]
    last_id: u64,
    #[serde(default)]
    facts: Vec<PersistFact>,
    #[serde(rename = "lastFactId", default)]
    last_fact_id: u64,
    #[serde(rename = "knowledgeBases", default)]
    knowledge_bases: Vec<PersistKb>,
}

impl Default for PersistStore {
    fn default() -> Self {
        Self {
            entries: Vec::new(),
            last_id: 0,
            facts: Vec::new(),
            last_fact_id: 0,
            knowledge_bases: Vec::new(),
        }
    }
}

// ---------------------------------------------------------------------------
// Conversion: Memory <-> PersistEntry
// ---------------------------------------------------------------------------

fn scope_to_namespace(scope: &MemoryScope, metadata: &HashMap<String, Value>) -> String {
    if let Some(Value::String(ns)) = metadata.get("namespace") {
        return ns.clone();
    }
    match scope {
        MemoryScope::Session => "session".to_string(),
        MemoryScope::Project => "default".to_string(),
        MemoryScope::Global => "global".to_string(),
    }
}

fn namespace_to_scope(ns: &str) -> MemoryScope {
    match ns {
        "session" => MemoryScope::Session,
        "global" => MemoryScope::Global,
        "default" => MemoryScope::Project,
        _ => MemoryScope::Project,
    }
}

fn memory_to_entry(m: &Memory) -> PersistEntry {
    let tags: Vec<String> = m.metadata.keys()
        .filter(|k| k.starts_with("tag:"))
        .map(|k| k.strip_prefix("tag:").unwrap().to_string())
        .collect();

    let key = m.metadata.get("key")
        .and_then(|v| v.as_str())
        .unwrap_or(&m.id)
        .to_string();

    let ttl = m.metadata.get("ttl")
        .and_then(|v| v.as_str())
        .map(|s| s.to_string());

    // Build clean metadata (exclude tags, key, ttl, namespace which are stored in dedicated fields)
    let clean_meta: HashMap<String, Value> = m.metadata.iter()
        .filter(|(k, _)| !k.starts_with("tag:") && *k != "key" && *k != "ttl" && *k != "namespace")
        .map(|(k, v)| (k.clone(), v.clone()))
        .collect();

    PersistEntry {
        id: m.id.clone(),
        key,
        value: m.content.clone(),
        tags,
        namespace: scope_to_namespace(&m.scope, &m.metadata),
        created: m.created_at.clone(),
        updated: m.updated_at.clone(),
        metadata: if clean_meta.is_empty() { None } else { Some(clean_meta) },
        ttl,
    }
}

fn entry_to_memory(e: &PersistEntry) -> Memory {
    let mut metadata: HashMap<String, Value> = e.metadata.clone().unwrap_or_default();

    // Store tags in metadata
    for tag in &e.tags {
        metadata.insert(format!("tag:{}", tag), json!(true));
    }

    // Store key in metadata
    metadata.insert("key".to_string(), json!(e.key));

    // Store TTL in metadata
    if let Some(ref ttl) = e.ttl {
        metadata.insert("ttl".to_string(), json!(ttl));
    }

    // Store namespace in metadata if not a standard scope name
    let scope = namespace_to_scope(&e.namespace);
    if e.namespace != "session" && e.namespace != "global" && e.namespace != "default" {
        metadata.insert("namespace".to_string(), json!(e.namespace));
    }

    Memory {
        id: e.id.clone(),
        content: e.value.clone(),
        scope,
        created_at: e.created.clone(),
        updated_at: e.updated.clone(),
        metadata,
    }
}

fn fact_to_persist(f: &Fact) -> PersistFact {
    PersistFact {
        id: f.id.clone(),
        content: f.content.clone(),
        kb_name: f.kb_name.clone(),
        scope: f.scope.to_string(),
        created: f.created_at.clone(),
    }
}

fn persist_to_fact(p: &PersistFact) -> Fact {
    Fact {
        id: p.id.clone(),
        content: p.content.clone(),
        kb_name: p.kb_name.clone(),
        scope: p.scope.parse().unwrap_or_default(),
        created_at: p.created.clone(),
    }
}

// ---------------------------------------------------------------------------
// TTL check
// ---------------------------------------------------------------------------

fn is_expired(metadata: &HashMap<String, Value>) -> bool {
    if let Some(Value::String(ttl)) = metadata.get("ttl") {
        if let Ok(expiry) = chrono::DateTime::parse_from_rfc3339(ttl) {
            return expiry < chrono::Utc::now();
        }
        // Try ISO date without timezone
        if let Ok(expiry) = chrono::NaiveDateTime::parse_from_str(ttl, "%Y-%m-%dT%H:%M:%S") {
            return expiry < chrono::Utc::now().naive_utc();
        }
    }
    false
}

fn entry_is_expired(e: &PersistEntry) -> bool {
    if let Some(ref ttl) = e.ttl {
        if let Ok(expiry) = chrono::DateTime::parse_from_rfc3339(ttl) {
            return expiry < chrono::Utc::now();
        }
        if let Ok(expiry) = chrono::NaiveDateTime::parse_from_str(ttl, "%Y-%m-%dT%H:%M:%S") {
            return expiry < chrono::Utc::now().naive_utc();
        }
    }
    false
}

// ---------------------------------------------------------------------------
// Arguments
// ---------------------------------------------------------------------------

/// Arguments for memory tool
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct MemoryToolArgs {
    #[serde(default)]
    pub action: String,
    /// Query for recall
    pub queries: Option<Vec<String>>,
    /// Single query
    pub query: Option<String>,
    /// Statements to store
    pub statements: Option<Vec<String>>,
    /// Single statement
    pub statement: Option<String>,
    /// Memory ID for update/delete
    pub id: Option<String>,
    /// Memory IDs for batch operations
    pub ids: Option<Vec<String>>,
    /// Updates for batch update
    pub updates: Option<Vec<Value>>,
    /// Scope
    pub scope: Option<String>,
    /// Namespace (alias for scope)
    pub namespace: Option<String>,
    /// Key for key-based storage
    pub key: Option<String>,
    /// TTL as ISO date string
    pub ttl: Option<String>,
    /// Limit results
    pub limit: Option<usize>,
    /// Knowledge base name
    pub kb_name: Option<String>,
    /// Facts to store
    pub facts: Option<Vec<String>>,
    /// Content to summarize
    pub content: Option<String>,
    /// Topic for summary
    pub topic: Option<String>,
    /// Metadata
    pub metadata: Option<HashMap<String, Value>>,
    /// Creations for manage
    pub creations: Option<Vec<String>>,
    /// Deletions for manage
    pub deletions: Option<Vec<String>>,
    /// Tag name for tag/untag
    pub tag: Option<String>,
    /// JSON data for import
    pub data: Option<String>,
    /// Description for kb create
    pub description: Option<String>,
    /// Sub-action for kb management
    pub sub_action: Option<String>,
}

// ---------------------------------------------------------------------------
// MemoryTool
// ---------------------------------------------------------------------------

/// Memory tool
pub struct MemoryTool {
    memories: Arc<RwLock<HashMap<String, Memory>>>,
    knowledge_bases: Arc<RwLock<HashMap<String, KnowledgeBase>>>,
    counter: Arc<RwLock<u64>>,
    fact_counter: Arc<RwLock<u64>>,
    history: Arc<RwLock<Vec<String>>>,
    loaded: Arc<RwLock<bool>>,
    storage_path: PathBuf,
}

impl MemoryTool {
    pub fn new() -> Self {
        Self::with_path(None)
    }

    /// Create with explicit storage path (useful for testing)
    pub fn with_path(path: Option<PathBuf>) -> Self {
        let storage_path = path.unwrap_or_else(|| {
            if let Ok(p) = std::env::var("MEMORY_PATH") {
                PathBuf::from(p)
            } else {
                dirs::home_dir()
                    .unwrap_or_else(|| PathBuf::from("."))
                    .join(".hanzo")
                    .join("memory.json")
            }
        });

        let tool = Self {
            memories: Arc::new(RwLock::new(HashMap::new())),
            knowledge_bases: Arc::new(RwLock::new(HashMap::new())),
            counter: Arc::new(RwLock::new(0)),
            fact_counter: Arc::new(RwLock::new(0)),
            history: Arc::new(RwLock::new(Vec::new())),
            loaded: Arc::new(RwLock::new(false)),
            storage_path,
        };

        // Load is async so we can't do it in the constructor.
        // The first execute() call will trigger load if needed.
        tool
    }

    /// Load from disk. Called lazily on first execute.
    async fn ensure_loaded(&self) -> Result<()> {
        {
            let loaded = self.loaded.read().await;
            if *loaded {
                return Ok(());
            }
        }
        self.load_from_disk().await?;
        *self.loaded.write().await = true;
        Ok(())
    }

    async fn load_from_disk(&self) -> Result<()> {
        let store = match tokio::fs::read_to_string(&self.storage_path).await {
            Ok(data) => {
                serde_json::from_str::<PersistStore>(&data).unwrap_or_default()
            }
            Err(_) => PersistStore::default(),
        };

        let mut memories = self.memories.write().await;
        let mut kbs = self.knowledge_bases.write().await;
        let mut counter = self.counter.write().await;
        let mut fact_counter = self.fact_counter.write().await;

        memories.clear();
        kbs.clear();

        *counter = store.last_id;
        *fact_counter = store.last_fact_id;

        // Load entries, filtering expired
        for entry in &store.entries {
            if entry_is_expired(entry) {
                continue;
            }
            let memory = entry_to_memory(entry);
            memories.insert(memory.id.clone(), memory);
        }

        // Load KB metadata first (so empty KBs are preserved)
        for pkb in &store.knowledge_bases {
            kbs.entry(pkb.name.clone()).or_insert_with(|| KnowledgeBase {
                name: pkb.name.clone(),
                description: pkb.description.clone(),
                scope: pkb.scope.parse().unwrap_or_default(),
                facts: Vec::new(),
                created_at: pkb.created.clone(),
            });
        }

        // Load facts into knowledge bases
        for pf in &store.facts {
            let fact = persist_to_fact(pf);
            let kb = kbs.entry(fact.kb_name.clone()).or_insert_with(|| KnowledgeBase {
                name: fact.kb_name.clone(),
                description: None,
                scope: fact.scope.clone(),
                facts: Vec::new(),
                created_at: fact.created_at.clone(),
            });
            kb.facts.push(fact);
        }

        Ok(())
    }

    async fn save_to_disk(&self) -> Result<()> {
        let memories = self.memories.read().await;
        let kbs = self.knowledge_bases.read().await;
        let counter = self.counter.read().await;
        let fact_counter = self.fact_counter.read().await;

        // Build entries, filtering expired
        let entries: Vec<PersistEntry> = memories.values()
            .filter(|m| !is_expired(&m.metadata))
            .map(|m| memory_to_entry(m))
            .collect();

        // Build facts from knowledge bases
        let facts: Vec<PersistFact> = kbs.values()
            .flat_map(|kb| kb.facts.iter().map(|f| fact_to_persist(f)))
            .collect();

        // Build KB metadata
        let knowledge_bases: Vec<PersistKb> = kbs.values()
            .map(|kb| PersistKb {
                name: kb.name.clone(),
                description: kb.description.clone(),
                scope: kb.scope.to_string(),
                created: kb.created_at.clone(),
            })
            .collect();

        let store = PersistStore {
            entries,
            last_id: *counter,
            facts,
            last_fact_id: *fact_counter,
            knowledge_bases,
        };

        // Ensure parent directory exists
        if let Some(parent) = self.storage_path.parent() {
            tokio::fs::create_dir_all(parent).await?;
        }

        let json = serde_json::to_string_pretty(&store)?;
        tokio::fs::write(&self.storage_path, json).await?;
        Ok(())
    }

    async fn next_id(&self, prefix: &str) -> String {
        let mut counter = self.counter.write().await;
        *counter += 1;
        format!("{}_{}", prefix, *counter)
    }

    async fn next_fact_id(&self) -> String {
        let mut counter = self.fact_counter.write().await;
        *counter += 1;
        format!("fact_{}", *counter)
    }

    /// Resolve scope from args: namespace takes priority, then scope, then default
    fn resolve_scope(args: &MemoryToolArgs) -> (MemoryScope, Option<String>) {
        if let Some(ref ns) = args.namespace {
            match ns.as_str() {
                "default" => (MemoryScope::Project, None),
                "session" => (MemoryScope::Session, None),
                "global" => (MemoryScope::Global, None),
                other => (MemoryScope::Project, Some(other.to_string())),
            }
        } else if let Some(ref s) = args.scope {
            (s.parse().unwrap_or_default(), None)
        } else {
            (MemoryScope::Project, None)
        }
    }

    /// Build metadata including key, ttl, namespace overrides
    fn build_metadata(args: &MemoryToolArgs, custom_namespace: &Option<String>) -> HashMap<String, Value> {
        let mut metadata = args.metadata.clone().unwrap_or_default();
        if let Some(ref key) = args.key {
            metadata.insert("key".to_string(), json!(key));
        }
        if let Some(ref ttl) = args.ttl {
            metadata.insert("ttl".to_string(), json!(ttl));
        }
        if let Some(ref ns) = custom_namespace {
            metadata.insert("namespace".to_string(), json!(ns));
        }
        metadata
    }

    pub async fn execute(&self, args: MemoryToolArgs) -> Result<String> {
        // Ensure data is loaded from disk on first call
        self.ensure_loaded().await?;

        let action: MemoryAction = if args.action.is_empty() {
            MemoryAction::Help
        } else {
            args.action.parse()?
        };

        let result = match action {
            MemoryAction::Recall => self.recall(args).await?,
            MemoryAction::Create => self.create(args).await?,
            MemoryAction::Update => self.update(args).await?,
            MemoryAction::Delete => self.delete(args).await?,
            MemoryAction::Manage => self.manage(args).await?,
            MemoryAction::Facts => self.facts(args).await?,
            MemoryAction::Summarize => self.summarize(args).await?,
            MemoryAction::List => self.list(args).await?,
            MemoryAction::Stats => self.stats(args).await?,
            MemoryAction::Clear => self.clear(args).await?,
            MemoryAction::Export => self.export_memories().await?,
            MemoryAction::Import => self.import_memories(args).await?,
            MemoryAction::Merge => self.merge_memories().await?,
            MemoryAction::Tag => self.tag_memory(args).await?,
            MemoryAction::Untag => self.untag_memory(args).await?,
            MemoryAction::Namespaces => self.namespaces().await?,
            MemoryAction::History => self.history_log().await?,
            MemoryAction::Kb => self.kb(args).await?,
            MemoryAction::Help => self.help()?,
        };

        Ok(serde_json::to_string(&result)?)
    }

    // -----------------------------------------------------------------------
    // Actions
    // -----------------------------------------------------------------------

    async fn recall(&self, args: MemoryToolArgs) -> Result<Value> {
        let queries = args.queries.clone()
            .or_else(|| args.query.clone().map(|q| vec![q]))
            .unwrap_or_default();
        let (scope, custom_ns) = Self::resolve_scope(&args);
        let limit = args.limit.unwrap_or(10);

        let memories = self.memories.read().await;
        let mut results = Vec::new();

        // Key-based recall: if key is provided, filter by key
        if let Some(ref key) = args.key {
            let matches: Vec<&Memory> = memories.values()
                .filter(|m| {
                    !is_expired(&m.metadata)
                        && m.metadata.get("key").and_then(|v| v.as_str()) == Some(key.as_str())
                        && Self::matches_scope_and_ns(m, &scope, &custom_ns)
                })
                .take(limit)
                .collect();

            for m in matches {
                results.push(memory_to_json(m));
            }

            return Ok(json!({
                "key": key,
                "scope": scope.to_string(),
                "results": results,
                "count": results.len()
            }));
        }

        // Query-based recall
        if queries.is_empty() {
            // No queries and no key — return recent memories in scope
            let matches: Vec<&Memory> = memories.values()
                .filter(|m| {
                    !is_expired(&m.metadata)
                        && Self::matches_scope_and_ns(m, &scope, &custom_ns)
                })
                .take(limit)
                .collect();
            for m in matches {
                results.push(memory_to_json(m));
            }
        } else {
            for query in &queries {
                let query_lower = query.to_lowercase();
                let terms: Vec<&str> = query_lower.split_whitespace().collect();
                let matches: Vec<&Memory> = memories.values()
                    .filter(|m| {
                        if is_expired(&m.metadata) { return false; }
                        if !Self::matches_scope_and_ns(m, &scope, &custom_ns) { return false; }
                        let text = m.content.to_lowercase();
                        terms.iter().all(|t| text.contains(t))
                    })
                    .take(limit)
                    .collect();

                for m in matches {
                    results.push(memory_to_json(m));
                }
            }
        }

        Ok(json!({
            "queries": queries,
            "scope": scope.to_string(),
            "results": results,
            "count": results.len()
        }))
    }

    async fn create(&self, args: MemoryToolArgs) -> Result<Value> {
        let statements = args.statements.clone()
            .or_else(|| args.statement.clone().map(|s| vec![s]))
            .or_else(|| args.content.clone().map(|c| vec![c]))
            .ok_or_else(|| anyhow!("statements required"))?;
        let (scope, custom_ns) = Self::resolve_scope(&args);
        let metadata = Self::build_metadata(&args, &custom_ns);
        let now = chrono::Utc::now().to_rfc3339();

        let mut created_ids = Vec::new();
        let mut memories = self.memories.write().await;

        for statement in statements {
            let id = self.next_id("mem").await;
            let memory = Memory {
                id: id.clone(),
                content: statement,
                scope: scope.clone(),
                created_at: now.clone(),
                updated_at: now.clone(),
                metadata: metadata.clone(),
            };
            memories.insert(id.clone(), memory);
            created_ids.push(id);
        }

        drop(memories);
        self.save_to_disk().await?;
        self.record_history(&format!("create: {} memories", created_ids.len())).await;

        Ok(json!({
            "created": created_ids.len(),
            "ids": created_ids,
            "scope": scope.to_string()
        }))
    }

    async fn update(&self, args: MemoryToolArgs) -> Result<Value> {
        let now = chrono::Utc::now().to_rfc3339();
        let mut updated_ids = Vec::new();

        // Single update by id + content/statement
        if let Some(ref id) = args.id {
            let new_content = args.statement.as_deref()
                .or(args.content.as_deref());
            if let Some(content) = new_content {
                let mut memories = self.memories.write().await;
                if let Some(memory) = memories.get_mut(id.as_str()) {
                    memory.content = content.to_string();
                    memory.updated_at = now.clone();
                    if let Some(ref ttl) = args.ttl {
                        memory.metadata.insert("ttl".to_string(), json!(ttl));
                    }
                    if let Some(ref key) = args.key {
                        memory.metadata.insert("key".to_string(), json!(key));
                    }
                    updated_ids.push(id.clone());
                }
                drop(memories);
                if !updated_ids.is_empty() {
                    self.save_to_disk().await?;
                }
                return Ok(json!({ "updated": updated_ids.len(), "ids": updated_ids }));
            }
        }

        // Batch updates
        let updates = args.updates.ok_or_else(|| anyhow!("updates or (id + statement) required"))?;
        let mut memories = self.memories.write().await;

        for update_val in updates {
            if let Some(obj) = update_val.as_object() {
                if let (Some(id), Some(statement)) = (
                    obj.get("id").and_then(|v| v.as_str()),
                    obj.get("statement").and_then(|v| v.as_str())
                ) {
                    if let Some(memory) = memories.get_mut(id) {
                        memory.content = statement.to_string();
                        memory.updated_at = now.clone();
                        updated_ids.push(id.to_string());
                    }
                }
            }
        }

        drop(memories);
        if !updated_ids.is_empty() {
            self.save_to_disk().await?;
        }
        self.record_history(&format!("update: {} memories", updated_ids.len())).await;

        Ok(json!({
            "updated": updated_ids.len(),
            "ids": updated_ids
        }))
    }

    async fn delete(&self, args: MemoryToolArgs) -> Result<Value> {
        let mut ids_to_delete = args.ids.clone()
            .or_else(|| args.id.clone().map(|id| vec![id]))
            .unwrap_or_default();

        // Delete by key
        if ids_to_delete.is_empty() {
            if let Some(ref key) = args.key {
                let memories = self.memories.read().await;
                ids_to_delete = memories.values()
                    .filter(|m| m.metadata.get("key").and_then(|v| v.as_str()) == Some(key.as_str()))
                    .map(|m| m.id.clone())
                    .collect();
            }
        }

        if ids_to_delete.is_empty() {
            return Err(anyhow!("ids, id, or key required"));
        }

        let mut deleted_ids = Vec::new();
        let mut memories = self.memories.write().await;

        for id in ids_to_delete {
            if memories.remove(&id).is_some() {
                deleted_ids.push(id);
            }
        }

        drop(memories);
        if !deleted_ids.is_empty() {
            self.save_to_disk().await?;
        }
        self.record_history(&format!("delete: {} memories", deleted_ids.len())).await;

        Ok(json!({
            "deleted": deleted_ids.len(),
            "ids": deleted_ids
        }))
    }

    async fn manage(&self, args: MemoryToolArgs) -> Result<Value> {
        let (scope, custom_ns) = Self::resolve_scope(&args);
        let metadata = Self::build_metadata(&args, &custom_ns);
        let now = chrono::Utc::now().to_rfc3339();

        let mut created_ids = Vec::new();
        let mut updated_ids = Vec::new();
        let mut deleted_ids = Vec::new();

        // Handle creations
        if let Some(creations) = args.creations.clone() {
            let mut memories = self.memories.write().await;
            for statement in creations {
                let id = self.next_id("mem").await;
                let memory = Memory {
                    id: id.clone(),
                    content: statement,
                    scope: scope.clone(),
                    created_at: now.clone(),
                    updated_at: now.clone(),
                    metadata: metadata.clone(),
                };
                memories.insert(id.clone(), memory);
                created_ids.push(id);
            }
        }

        // Handle updates
        if let Some(updates) = args.updates.clone() {
            let mut memories = self.memories.write().await;
            for update_val in updates {
                if let Some(obj) = update_val.as_object() {
                    if let (Some(id), Some(statement)) = (
                        obj.get("id").and_then(|v| v.as_str()),
                        obj.get("statement").and_then(|v| v.as_str())
                    ) {
                        if let Some(memory) = memories.get_mut(id) {
                            memory.content = statement.to_string();
                            memory.updated_at = now.clone();
                            updated_ids.push(id.to_string());
                        }
                    }
                }
            }
        }

        // Handle deletions
        if let Some(deletions) = args.deletions.clone() {
            let mut memories = self.memories.write().await;
            for id in deletions {
                if memories.remove(&id).is_some() {
                    deleted_ids.push(id);
                }
            }
        }

        let mutated = !created_ids.is_empty() || !updated_ids.is_empty() || !deleted_ids.is_empty();
        if mutated {
            self.save_to_disk().await?;
        }

        Ok(json!({
            "created": created_ids,
            "updated": updated_ids,
            "deleted": deleted_ids,
            "scope": scope.to_string()
        }))
    }

    async fn facts(&self, args: MemoryToolArgs) -> Result<Value> {
        let kb_name = args.kb_name.clone().unwrap_or_else(|| "general".to_string());
        let (scope, _custom_ns) = Self::resolve_scope(&args);
        let now = chrono::Utc::now().to_rfc3339();

        if let Some(new_facts) = args.facts.clone() {
            // Store facts
            let mut kbs = self.knowledge_bases.write().await;
            let kb = kbs.entry(kb_name.clone()).or_insert_with(|| KnowledgeBase {
                name: kb_name.clone(),
                description: None,
                scope: scope.clone(),
                facts: Vec::new(),
                created_at: now.clone(),
            });

            let mut created_ids = Vec::new();
            for fact_content in new_facts {
                let id = self.next_fact_id().await;
                let fact = Fact {
                    id: id.clone(),
                    content: fact_content,
                    kb_name: kb_name.clone(),
                    scope: scope.clone(),
                    created_at: now.clone(),
                };
                kb.facts.push(fact);
                created_ids.push(id);
            }

            drop(kbs);
            self.save_to_disk().await?;

            return Ok(json!({
                "stored": created_ids.len(),
                "ids": created_ids,
                "kb_name": kb_name
            }));
        }

        // Recall facts
        if let Some(queries) = args.queries.clone().or_else(|| args.query.clone().map(|q| vec![q])) {
            let kbs = self.knowledge_bases.read().await;
            let limit = args.limit.unwrap_or(10);
            let mut results = Vec::new();

            if let Some(kb) = kbs.get(&kb_name) {
                for query in &queries {
                    let query_lower = query.to_lowercase();
                    let matches: Vec<&Fact> = kb.facts.iter()
                        .filter(|f| f.content.to_lowercase().contains(&query_lower))
                        .take(limit)
                        .collect();

                    for f in matches {
                        results.push(json!({
                            "id": f.id,
                            "content": f.content,
                            "kb_name": f.kb_name
                        }));
                    }
                }
            }

            return Ok(json!({
                "queries": queries,
                "kb_name": kb_name,
                "results": results,
                "count": results.len()
            }));
        }

        // List knowledge bases
        let kbs = self.knowledge_bases.read().await;
        let kb_list: Vec<Value> = kbs.values()
            .map(|kb| json!({
                "name": kb.name,
                "description": kb.description,
                "fact_count": kb.facts.len(),
                "scope": kb.scope.to_string()
            }))
            .collect();

        Ok(json!({
            "knowledge_bases": kb_list,
            "count": kb_list.len()
        }))
    }

    async fn summarize(&self, args: MemoryToolArgs) -> Result<Value> {
        let content = args.content.clone().ok_or_else(|| anyhow!("content required"))?;
        let topic = args.topic.clone().ok_or_else(|| anyhow!("topic required"))?;
        let (scope, custom_ns) = Self::resolve_scope(&args);
        let now = chrono::Utc::now().to_rfc3339();

        // Create memory from summary
        let id = self.next_id("mem").await;
        let summary = format!("[{}] {}", topic, content);
        let mut metadata = Self::build_metadata(&args, &custom_ns);
        metadata.insert("topic".to_string(), json!(topic));
        metadata.insert("type".to_string(), json!("summary"));

        let memory = Memory {
            id: id.clone(),
            content: summary.clone(),
            scope,
            created_at: now.clone(),
            updated_at: now,
            metadata,
        };

        self.memories.write().await.insert(id.clone(), memory);
        self.save_to_disk().await?;

        // Extract key facts (simplified)
        let facts: Vec<&str> = content.lines()
            .filter(|l| !l.trim().is_empty())
            .take(5)
            .collect();

        Ok(json!({
            "id": id,
            "topic": topic,
            "stored": true,
            "extracted_facts": facts.len(),
            "facts": facts
        }))
    }

    async fn list(&self, args: MemoryToolArgs) -> Result<Value> {
        let (scope, custom_ns) = Self::resolve_scope(&args);
        let has_scope_filter = args.scope.is_some() || args.namespace.is_some();
        let limit = args.limit.unwrap_or(50);

        let memories = self.memories.read().await;
        let results: Vec<Value> = memories.values()
            .filter(|m| {
                if is_expired(&m.metadata) { return false; }
                if has_scope_filter {
                    Self::matches_scope_and_ns(m, &scope, &custom_ns)
                } else {
                    true
                }
            })
            .take(limit)
            .map(|m| memory_to_json(m))
            .collect();

        Ok(json!({
            "memories": results,
            "count": results.len(),
            "total": memories.len()
        }))
    }

    async fn record_history(&self, entry: &str) {
        let mut history = self.history.write().await;
        history.push(format!("[{}] {}", chrono::Utc::now().to_rfc3339(), entry));
        if history.len() > 1000 { history.drain(0..500); }
    }

    async fn stats(&self, _args: MemoryToolArgs) -> Result<Value> {
        let memories = self.memories.read().await;
        let kbs = self.knowledge_bases.read().await;
        let mut by_scope: HashMap<String, usize> = HashMap::new();
        let mut by_namespace: HashMap<String, usize> = HashMap::new();
        let mut total_size = 0usize;
        for m in memories.values() {
            if is_expired(&m.metadata) { continue; }
            let scope_key = m.scope.to_string();
            *by_scope.entry(scope_key).or_insert(0) += 1;
            let ns = scope_to_namespace(&m.scope, &m.metadata);
            *by_namespace.entry(ns).or_insert(0) += 1;
            total_size += m.content.len();
        }
        Ok(json!({
            "total_memories": memories.len(),
            "total_size_bytes": total_size,
            "by_scope": by_scope,
            "by_namespace": by_namespace,
            "knowledge_bases": kbs.len(),
            "total_facts": kbs.values().map(|kb| kb.facts.len()).sum::<usize>(),
            "storage_path": self.storage_path.to_string_lossy()
        }))
    }

    async fn clear(&self, args: MemoryToolArgs) -> Result<Value> {
        let (scope, custom_ns) = Self::resolve_scope(&args);
        let has_scope_filter = args.scope.is_some() || args.namespace.is_some();
        let mut memories = self.memories.write().await;
        let before = memories.len();
        if has_scope_filter {
            memories.retain(|_, m| !Self::matches_scope_and_ns(m, &scope, &custom_ns));
        } else {
            memories.clear();
        }
        let cleared = before - memories.len();
        drop(memories);
        self.save_to_disk().await?;
        self.record_history(&format!("clear: removed {} memories", cleared)).await;
        Ok(json!({ "cleared": cleared, "remaining": before - cleared }))
    }

    async fn export_memories(&self) -> Result<Value> {
        let memories = self.memories.read().await;
        let kbs = self.knowledge_bases.read().await;
        let mem_list: Vec<Value> = memories.values()
            .filter(|m| !is_expired(&m.metadata))
            .map(|m| memory_to_json(m))
            .collect();
        let kb_list: Vec<Value> = kbs.values().map(|kb| json!({
            "name": kb.name, "fact_count": kb.facts.len(),
            "scope": kb.scope.to_string()
        })).collect();
        Ok(json!({
            "memories": mem_list,
            "knowledge_bases": kb_list,
            "count": mem_list.len(),
            "storage_path": self.storage_path.to_string_lossy()
        }))
    }

    async fn import_memories(&self, args: MemoryToolArgs) -> Result<Value> {
        let data = args.data.clone().ok_or_else(|| anyhow!("data (JSON string) required"))?;
        let parsed: Value = serde_json::from_str(&data)?;
        let now = chrono::Utc::now().to_rfc3339();
        let mut imported = 0;
        let mut memories = self.memories.write().await;
        if let Some(items) = parsed.get("memories").and_then(|v| v.as_array()) {
            for item in items {
                let id = self.next_id("mem").await;
                let content = item.get("content").and_then(|v| v.as_str())
                    .or_else(|| item.get("value").and_then(|v| v.as_str()))
                    .unwrap_or("").to_string();
                let scope: MemoryScope = item.get("scope").and_then(|v| v.as_str())
                    .unwrap_or("project").parse()?;
                let mut metadata = HashMap::new();
                if let Some(key) = item.get("key").and_then(|v| v.as_str()) {
                    metadata.insert("key".to_string(), json!(key));
                }
                let memory = Memory {
                    id: id.clone(), content, scope,
                    created_at: now.clone(), updated_at: now.clone(),
                    metadata,
                };
                memories.insert(id, memory);
                imported += 1;
            }
        }
        // Also import from "entries" key (TypeScript format)
        if let Some(items) = parsed.get("entries").and_then(|v| v.as_array()) {
            for item in items {
                if let Ok(entry) = serde_json::from_value::<PersistEntry>(item.clone()) {
                    if entry_is_expired(&entry) { continue; }
                    let memory = entry_to_memory(&entry);
                    let id = self.next_id("mem").await;
                    let mut m = memory;
                    m.id = id.clone();
                    memories.insert(id, m);
                    imported += 1;
                }
            }
        }
        drop(memories);
        self.save_to_disk().await?;
        self.record_history(&format!("import: {} memories", imported)).await;
        Ok(json!({ "imported": imported }))
    }

    async fn merge_memories(&self) -> Result<Value> {
        let mut memories = self.memories.write().await;
        let ids: Vec<String> = memories.keys().cloned().collect();
        let mut merged = 0;
        let mut to_remove = Vec::new();
        for i in 0..ids.len() {
            for j in (i+1)..ids.len() {
                if to_remove.contains(&ids[j]) { continue; }
                let a = memories.get(&ids[i]).map(|m| m.content.clone());
                let b = memories.get(&ids[j]).map(|m| m.content.clone());
                if let (Some(a), Some(b)) = (a, b) {
                    if a == b {
                        to_remove.push(ids[j].clone());
                        merged += 1;
                    }
                }
            }
        }
        for id in &to_remove { memories.remove(id); }
        drop(memories);
        if merged > 0 {
            self.save_to_disk().await?;
        }
        self.record_history(&format!("merge: removed {} duplicates", merged)).await;
        Ok(json!({ "merged": merged, "removed_ids": to_remove }))
    }

    async fn tag_memory(&self, args: MemoryToolArgs) -> Result<Value> {
        let id = args.id.clone().ok_or_else(|| anyhow!("id required"))?;
        let tag = args.tag.clone().ok_or_else(|| anyhow!("tag required"))?;
        let mut memories = self.memories.write().await;
        let memory = memories.get_mut(&id).ok_or_else(|| anyhow!("Memory not found: {}", id))?;
        memory.metadata.insert(format!("tag:{}", tag), json!(true));
        drop(memories);
        self.save_to_disk().await?;
        self.record_history(&format!("tag: {} += {}", id, tag)).await;
        Ok(json!({ "id": id, "tag": tag, "tagged": true }))
    }

    async fn untag_memory(&self, args: MemoryToolArgs) -> Result<Value> {
        let id = args.id.clone().ok_or_else(|| anyhow!("id required"))?;
        let tag = args.tag.clone().ok_or_else(|| anyhow!("tag required"))?;
        let mut memories = self.memories.write().await;
        let memory = memories.get_mut(&id).ok_or_else(|| anyhow!("Memory not found: {}", id))?;
        memory.metadata.remove(&format!("tag:{}", tag));
        drop(memories);
        self.save_to_disk().await?;
        self.record_history(&format!("untag: {} -= {}", id, tag)).await;
        Ok(json!({ "id": id, "tag": tag, "untagged": true }))
    }

    async fn namespaces(&self) -> Result<Value> {
        let memories = self.memories.read().await;
        let mut ns_counts: HashMap<String, usize> = HashMap::new();
        for m in memories.values() {
            if is_expired(&m.metadata) { continue; }
            let ns = scope_to_namespace(&m.scope, &m.metadata);
            *ns_counts.entry(ns).or_insert(0) += 1;
        }
        // Also include knowledge base names
        let kbs = self.knowledge_bases.read().await;
        let kb_names: Vec<String> = kbs.keys().cloned().collect();

        Ok(json!({
            "namespaces": ns_counts,
            "knowledge_bases": kb_names,
            "count": ns_counts.len()
        }))
    }

    async fn history_log(&self) -> Result<Value> {
        let history = self.history.read().await;
        let entries: Vec<&String> = history.iter().rev().take(50).collect();
        Ok(json!({ "history": entries, "count": entries.len() }))
    }

    async fn kb(&self, args: MemoryToolArgs) -> Result<Value> {
        let sub = args.sub_action.as_deref()
            .or(args.query.as_deref())
            .unwrap_or("list");

        match sub {
            "create" => {
                let name = args.kb_name.clone()
                    .ok_or_else(|| anyhow!("kb_name required for kb create"))?;
                let (scope, _) = Self::resolve_scope(&args);
                let now = chrono::Utc::now().to_rfc3339();
                let mut kbs = self.knowledge_bases.write().await;
                if kbs.contains_key(&name) {
                    return Ok(json!({ "error": format!("Knowledge base '{}' already exists", name) }));
                }
                kbs.insert(name.clone(), KnowledgeBase {
                    name: name.clone(),
                    description: args.description.clone(),
                    scope,
                    facts: Vec::new(),
                    created_at: now,
                });
                drop(kbs);
                self.save_to_disk().await?;
                Ok(json!({ "created": name, "description": args.description }))
            }
            "delete" => {
                let name = args.kb_name.clone()
                    .ok_or_else(|| anyhow!("kb_name required for kb delete"))?;
                let mut kbs = self.knowledge_bases.write().await;
                if kbs.remove(&name).is_some() {
                    drop(kbs);
                    self.save_to_disk().await?;
                    Ok(json!({ "deleted": name }))
                } else {
                    Ok(json!({ "error": format!("Knowledge base '{}' not found", name) }))
                }
            }
            "list" | _ => {
                let kbs = self.knowledge_bases.read().await;
                let list: Vec<Value> = kbs.values().map(|kb| json!({
                    "name": kb.name,
                    "description": kb.description,
                    "fact_count": kb.facts.len(),
                    "scope": kb.scope.to_string(),
                    "created_at": kb.created_at
                })).collect();
                Ok(json!({ "knowledge_bases": list, "count": list.len() }))
            }
        }
    }

    fn help(&self) -> Result<Value> {
        Ok(json!({
            "name": "memory",
            "version": "0.13.0",
            "description": "Persistent memory and knowledge management tool (HIP-0300). Data persisted to ~/.hanzo/memory.json, compatible with TypeScript runtime.",
            "actions": {
                "recall": {
                    "description": "Search memories by query or key",
                    "params": ["queries|query", "key", "scope|namespace", "limit"]
                },
                "create": {
                    "description": "Store new memories",
                    "params": ["statements|statement|content", "scope|namespace", "key", "ttl", "metadata"]
                },
                "update": {
                    "description": "Update existing memories",
                    "params": ["id + statement|content", "updates[]", "key", "ttl"]
                },
                "delete": {
                    "description": "Remove memories by id, ids, or key",
                    "params": ["id|ids|key"]
                },
                "manage": {
                    "description": "Atomic create/update/delete in one call",
                    "params": ["creations[]", "updates[]", "deletions[]", "scope|namespace", "key", "ttl"]
                },
                "facts": {
                    "description": "Store/recall knowledge base facts",
                    "params": ["kb_name", "facts[]", "queries|query", "scope", "limit"]
                },
                "summarize": {
                    "description": "Summarize content and store as memory",
                    "params": ["content", "topic", "scope|namespace"]
                },
                "list": {
                    "description": "List all memories, optionally filtered by scope/namespace",
                    "params": ["scope|namespace", "limit"]
                },
                "stats": {
                    "description": "Memory statistics by scope and namespace",
                    "params": []
                },
                "clear": {
                    "description": "Clear memories (optional scope/namespace filter)",
                    "params": ["scope|namespace"]
                },
                "export": {
                    "description": "Export all memories as JSON",
                    "params": []
                },
                "import": {
                    "description": "Import memories from JSON data string",
                    "params": ["data"]
                },
                "merge": {
                    "description": "Remove duplicate memories",
                    "params": []
                },
                "tag": {
                    "description": "Add tag to a memory",
                    "params": ["id", "tag"]
                },
                "untag": {
                    "description": "Remove tag from a memory",
                    "params": ["id", "tag"]
                },
                "namespaces": {
                    "description": "List all namespaces with entry counts",
                    "params": []
                },
                "history": {
                    "description": "Recent operation history",
                    "params": []
                },
                "kb": {
                    "description": "Knowledge base management (create/list/delete)",
                    "params": ["sub_action (create|list|delete)", "kb_name", "description"]
                },
                "help": {
                    "description": "Show this documentation",
                    "params": []
                }
            },
            "scopes": ["session", "project", "global"],
            "namespace_mapping": {
                "default": "project scope",
                "session": "session scope",
                "global": "global scope",
                "<custom>": "project scope with namespace stored in metadata"
            },
            "persistence": "~/.hanzo/memory.json (auto-loaded, saved after every mutation)"
        }))
    }

    // -----------------------------------------------------------------------
    // Helpers
    // -----------------------------------------------------------------------

    fn matches_scope_and_ns(m: &Memory, scope: &MemoryScope, custom_ns: &Option<String>) -> bool {
        if m.scope != *scope {
            return false;
        }
        if let Some(ref ns) = custom_ns {
            // Must match the custom namespace in metadata
            m.metadata.get("namespace")
                .and_then(|v| v.as_str())
                .map_or(false, |v| v == ns.as_str())
        } else {
            // Standard scope — should NOT have a custom namespace
            !m.metadata.contains_key("namespace")
        }
    }
}

fn memory_to_json(m: &Memory) -> Value {
    let ns = scope_to_namespace(&m.scope, &m.metadata);
    let key = m.metadata.get("key").and_then(|v| v.as_str()).unwrap_or(&m.id);
    let tags: Vec<String> = m.metadata.keys()
        .filter(|k| k.starts_with("tag:"))
        .map(|k| k.strip_prefix("tag:").unwrap().to_string())
        .collect();

    json!({
        "id": m.id,
        "key": key,
        "content": m.content,
        "scope": m.scope.to_string(),
        "namespace": ns,
        "tags": tags,
        "created_at": m.created_at,
        "updated_at": m.updated_at,
        "metadata": m.metadata
    })
}

// ---------------------------------------------------------------------------
// MCP Tool Definition
// ---------------------------------------------------------------------------

#[derive(Debug, Serialize, Deserialize)]
pub struct MemoryToolDefinition {
    pub name: String,
    pub description: String,
    pub input_schema: Value,
}

impl MemoryToolDefinition {
    pub fn new() -> Self {
        Self {
            name: "memory".to_string(),
            description: r#"Persistent memory and knowledge management tool (HIP-0300).
Data persisted to ~/.hanzo/memory.json (cross-runtime compatible).

Actions:
- recall: Search memories by query or key
- create: Store new memories (with optional key, ttl, namespace)
- update: Update existing memories
- delete: Remove memories by id or key
- manage: Atomic create/update/delete
- facts: Manage knowledge base facts
- summarize: Summarize and store information
- list: List all memories
- stats: Memory statistics
- kb: Knowledge base management (create/list/delete)
- help: Show all actions and parameters

Scopes: session, project, global
Namespaces: alias for scope — "default"->project, "session"->session, "global"->global, custom->project+metadata"#.to_string(),
            input_schema: json!({
                "type": "object",
                "properties": {
                    "action": {
                        "type": "string",
                        "enum": ["recall", "create", "update", "delete", "manage", "facts", "summarize", "list", "stats", "clear", "export", "import", "merge", "tag", "untag", "namespaces", "history", "kb", "help"]
                    },
                    "queries": {"type": "array", "items": {"type": "string"}},
                    "query": {"type": "string"},
                    "statements": {"type": "array", "items": {"type": "string"}},
                    "statement": {"type": "string"},
                    "id": {"type": "string"},
                    "ids": {"type": "array", "items": {"type": "string"}},
                    "updates": {"type": "array", "items": {"type": "object"}},
                    "scope": {"type": "string", "enum": ["session", "project", "global"]},
                    "namespace": {"type": "string", "description": "Namespace alias for scope. 'default'->project, 'session'->session, 'global'->global, custom->project with namespace in metadata"},
                    "key": {"type": "string", "description": "Key for key-based storage and recall"},
                    "ttl": {"type": "string", "description": "Expiry as ISO date string (e.g. 2026-03-25T00:00:00Z)"},
                    "limit": {"type": "integer"},
                    "kb_name": {"type": "string"},
                    "facts": {"type": "array", "items": {"type": "string"}},
                    "content": {"type": "string"},
                    "topic": {"type": "string"},
                    "metadata": {"type": "object"},
                    "creations": {"type": "array", "items": {"type": "string"}},
                    "deletions": {"type": "array", "items": {"type": "string"}},
                    "tag": {"type": "string", "description": "Tag name for tag/untag"},
                    "data": {"type": "string", "description": "JSON data for import"},
                    "description": {"type": "string", "description": "Description for kb create"},
                    "sub_action": {"type": "string", "description": "Sub-action for kb (create|list|delete)"}
                }
            }),
        }
    }
}

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

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

    fn test_tool() -> (MemoryTool, NamedTempFile) {
        let tmp = NamedTempFile::new().unwrap();
        let tool = MemoryTool::with_path(Some(tmp.path().to_path_buf()));
        (tool, tmp)
    }

    #[tokio::test]
    async fn test_create_memory() {
        let (tool, _tmp) = test_tool();
        let args = MemoryToolArgs {
            action: "create".to_string(),
            statements: Some(vec!["User prefers dark mode".to_string()]),
            scope: Some("project".to_string()),
            ..Default::default()
        };

        let result = tool.execute(args).await;
        assert!(result.is_ok());
        let output = result.unwrap();
        assert!(output.contains("created"));
    }

    #[tokio::test]
    async fn test_recall_memory() {
        let (tool, _tmp) = test_tool();

        // Create first
        let args = MemoryToolArgs {
            action: "create".to_string(),
            statements: Some(vec!["User prefers Python".to_string()]),
            ..Default::default()
        };
        tool.execute(args).await.unwrap();

        // Recall
        let args = MemoryToolArgs {
            action: "recall".to_string(),
            queries: Some(vec!["Python".to_string()]),
            ..Default::default()
        };

        let result = tool.execute(args).await;
        assert!(result.is_ok());
        let output = result.unwrap();
        assert!(output.contains("Python"));
    }

    #[tokio::test]
    async fn test_facts() {
        let (tool, _tmp) = test_tool();
        let args = MemoryToolArgs {
            action: "facts".to_string(),
            kb_name: Some("coding".to_string()),
            facts: Some(vec!["Use uv for Python".to_string()]),
            ..Default::default()
        };

        let result = tool.execute(args).await;
        assert!(result.is_ok());
        let output = result.unwrap();
        assert!(output.contains("stored"));
    }

    #[tokio::test]
    async fn test_summarize() {
        let (tool, _tmp) = test_tool();
        let args = MemoryToolArgs {
            action: "summarize".to_string(),
            content: Some("Discussion about API design patterns and best practices.".to_string()),
            topic: Some("API Design".to_string()),
            ..Default::default()
        };

        let result = tool.execute(args).await;
        assert!(result.is_ok());
        let output = result.unwrap();
        assert!(output.contains("API Design"));
    }

    #[tokio::test]
    async fn test_persistence_survives_reload() {
        let tmp = NamedTempFile::new().unwrap();
        let path = tmp.path().to_path_buf();

        // Create with first tool instance
        {
            let tool = MemoryTool::with_path(Some(path.clone()));
            let args = MemoryToolArgs {
                action: "create".to_string(),
                statements: Some(vec!["Persistent memory test".to_string()]),
                key: Some("test-key".to_string()),
                ..Default::default()
            };
            tool.execute(args).await.unwrap();
        }

        // Read with second tool instance (simulating restart)
        {
            let tool = MemoryTool::with_path(Some(path.clone()));
            let args = MemoryToolArgs {
                action: "recall".to_string(),
                key: Some("test-key".to_string()),
                ..Default::default()
            };
            let result = tool.execute(args).await.unwrap();
            assert!(result.contains("Persistent memory test"), "Memory should survive restart: {}", result);
        }
    }

    #[tokio::test]
    async fn test_key_based_storage_and_recall() {
        let (tool, _tmp) = test_tool();

        // Create with key
        let args = MemoryToolArgs {
            action: "create".to_string(),
            statements: Some(vec!["Blue agent report for task 42".to_string()]),
            key: Some("blue-report-42".to_string()),
            ..Default::default()
        };
        tool.execute(args).await.unwrap();

        // Recall by key
        let args = MemoryToolArgs {
            action: "recall".to_string(),
            key: Some("blue-report-42".to_string()),
            ..Default::default()
        };
        let result = tool.execute(args).await.unwrap();
        assert!(result.contains("Blue agent report"), "Key recall should work: {}", result);
    }

    #[tokio::test]
    async fn test_namespace_support() {
        let (tool, _tmp) = test_tool();

        // Create with custom namespace
        let args = MemoryToolArgs {
            action: "create".to_string(),
            statements: Some(vec!["Blue-red coordination data".to_string()]),
            namespace: Some("blue-red".to_string()),
            ..Default::default()
        };
        tool.execute(args).await.unwrap();

        // Recall with same namespace
        let args = MemoryToolArgs {
            action: "recall".to_string(),
            queries: Some(vec!["coordination".to_string()]),
            namespace: Some("blue-red".to_string()),
            ..Default::default()
        };
        let result = tool.execute(args).await.unwrap();
        assert!(result.contains("coordination"), "Namespace recall should work: {}", result);

        // Recall with different namespace should NOT find it
        let args = MemoryToolArgs {
            action: "recall".to_string(),
            queries: Some(vec!["coordination".to_string()]),
            namespace: Some("other".to_string()),
            ..Default::default()
        };
        let result = tool.execute(args).await.unwrap();
        let parsed: Value = serde_json::from_str(&result).unwrap();
        assert_eq!(parsed["count"], 0, "Different namespace should not find memory");
    }

    #[tokio::test]
    async fn test_ttl_expiry() {
        let (tool, _tmp) = test_tool();

        // Create with expired TTL
        let args = MemoryToolArgs {
            action: "create".to_string(),
            statements: Some(vec!["Should be expired".to_string()]),
            ttl: Some("2020-01-01T00:00:00Z".to_string()),
            ..Default::default()
        };
        tool.execute(args).await.unwrap();

        // Create with future TTL
        let args = MemoryToolArgs {
            action: "create".to_string(),
            statements: Some(vec!["Should be visible".to_string()]),
            ttl: Some("2099-01-01T00:00:00Z".to_string()),
            ..Default::default()
        };
        tool.execute(args).await.unwrap();

        // List should only show non-expired
        let args = MemoryToolArgs {
            action: "list".to_string(),
            ..Default::default()
        };
        let result = tool.execute(args).await.unwrap();
        assert!(result.contains("Should be visible"), "Non-expired should be visible");
        assert!(!result.contains("Should be expired"), "Expired should be filtered: {}", result);
    }

    #[tokio::test]
    async fn test_kb_management() {
        let (tool, _tmp) = test_tool();

        // Create KB
        let args = MemoryToolArgs {
            action: "kb".to_string(),
            sub_action: Some("create".to_string()),
            kb_name: Some("test-kb".to_string()),
            description: Some("Test knowledge base".to_string()),
            ..Default::default()
        };
        let result = tool.execute(args).await.unwrap();
        assert!(result.contains("test-kb"), "KB should be created: {}", result);

        // List KBs
        let args = MemoryToolArgs {
            action: "kb".to_string(),
            sub_action: Some("list".to_string()),
            ..Default::default()
        };
        let result = tool.execute(args).await.unwrap();
        assert!(result.contains("test-kb"), "KB should be listed: {}", result);

        // Delete KB
        let args = MemoryToolArgs {
            action: "kb".to_string(),
            sub_action: Some("delete".to_string()),
            kb_name: Some("test-kb".to_string()),
            ..Default::default()
        };
        let result = tool.execute(args).await.unwrap();
        assert!(result.contains("test-kb"), "KB should be deleted: {}", result);

        // Verify deleted
        let args = MemoryToolArgs {
            action: "kb".to_string(),
            sub_action: Some("list".to_string()),
            ..Default::default()
        };
        let result = tool.execute(args).await.unwrap();
        let parsed: Value = serde_json::from_str(&result).unwrap();
        assert_eq!(parsed["count"], 0, "KB list should be empty after delete");
    }

    #[tokio::test]
    async fn test_help_action() {
        let (tool, _tmp) = test_tool();
        let args = MemoryToolArgs {
            action: "help".to_string(),
            ..Default::default()
        };
        let result = tool.execute(args).await.unwrap();
        assert!(result.contains("recall"));
        assert!(result.contains("create"));
        assert!(result.contains("kb"));
        assert!(result.contains("persistence"));
    }

    #[tokio::test]
    async fn test_delete_by_key() {
        let (tool, _tmp) = test_tool();

        // Create with key
        let args = MemoryToolArgs {
            action: "create".to_string(),
            statements: Some(vec!["Deletable memory".to_string()]),
            key: Some("delete-me".to_string()),
            ..Default::default()
        };
        tool.execute(args).await.unwrap();

        // Delete by key
        let args = MemoryToolArgs {
            action: "delete".to_string(),
            key: Some("delete-me".to_string()),
            ..Default::default()
        };
        let result = tool.execute(args).await.unwrap();
        assert!(result.contains("\"deleted\":1"), "Should delete by key: {}", result);

        // Verify gone
        let args = MemoryToolArgs {
            action: "recall".to_string(),
            key: Some("delete-me".to_string()),
            ..Default::default()
        };
        let result = tool.execute(args).await.unwrap();
        let parsed: Value = serde_json::from_str(&result).unwrap();
        assert_eq!(parsed["count"], 0, "Deleted memory should not be found");
    }

    #[tokio::test]
    async fn test_json_format_compatibility() {
        let tmp = NamedTempFile::new().unwrap();
        let path = tmp.path().to_path_buf();

        // Create with Rust tool
        {
            let tool = MemoryTool::with_path(Some(path.clone()));
            let args = MemoryToolArgs {
                action: "create".to_string(),
                statements: Some(vec!["Cross-runtime test".to_string()]),
                key: Some("compat-key".to_string()),
                namespace: Some("blue-red".to_string()),
                ..Default::default()
            };
            tool.execute(args).await.unwrap();
        }

        // Read raw JSON and verify TypeScript-compatible format
        let data = tokio::fs::read_to_string(&path).await.unwrap();
        let store: Value = serde_json::from_str(&data).unwrap();

        assert!(store.get("entries").is_some(), "Should have entries key");
        assert!(store.get("lastId").is_some(), "Should have lastId key");
        assert!(store.get("facts").is_some(), "Should have facts key");
        assert!(store.get("lastFactId").is_some(), "Should have lastFactId key");

        let entry = &store["entries"][0];
        assert!(entry.get("key").is_some(), "Entry should have key");
        assert!(entry.get("value").is_some(), "Entry should have value");
        assert!(entry.get("namespace").is_some(), "Entry should have namespace");
        assert!(entry.get("tags").is_some(), "Entry should have tags");
        assert!(entry.get("created").is_some(), "Entry should have created");
        assert!(entry.get("updated").is_some(), "Entry should have updated");

        assert_eq!(entry["key"], "compat-key");
        assert_eq!(entry["value"], "Cross-runtime test");
        assert_eq!(entry["namespace"], "blue-red");
    }

    #[tokio::test]
    async fn test_namespace_default_maps_to_project() {
        let (tool, _tmp) = test_tool();

        // Create with namespace "default" (should map to Project scope)
        let args = MemoryToolArgs {
            action: "create".to_string(),
            statements: Some(vec!["Default namespace test".to_string()]),
            namespace: Some("default".to_string()),
            ..Default::default()
        };
        tool.execute(args).await.unwrap();

        // Recall with scope "project" should find it
        let args = MemoryToolArgs {
            action: "recall".to_string(),
            queries: Some(vec!["Default namespace".to_string()]),
            scope: Some("project".to_string()),
            ..Default::default()
        };
        let result = tool.execute(args).await.unwrap();
        assert!(result.contains("Default namespace test"), "default namespace should map to project scope: {}", result);
    }

    #[tokio::test]
    async fn test_persistence_with_facts() {
        let tmp = NamedTempFile::new().unwrap();
        let path = tmp.path().to_path_buf();

        // Store facts with first instance
        {
            let tool = MemoryTool::with_path(Some(path.clone()));
            let args = MemoryToolArgs {
                action: "facts".to_string(),
                kb_name: Some("coding".to_string()),
                facts: Some(vec!["Always use uv for Python".to_string()]),
                ..Default::default()
            };
            tool.execute(args).await.unwrap();
        }

        // Load with second instance and query
        {
            let tool = MemoryTool::with_path(Some(path.clone()));
            let args = MemoryToolArgs {
                action: "facts".to_string(),
                kb_name: Some("coding".to_string()),
                query: Some("uv".to_string()),
                ..Default::default()
            };
            let result = tool.execute(args).await.unwrap();
            assert!(result.contains("Always use uv"), "Facts should survive restart: {}", result);
        }
    }
}