ruvllm 2.1.0

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

use crate::error::Result;
use rand::{rngs::StdRng, seq::SliceRandom, Rng, SeedableRng};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::Path;

/// MCP Tool categories for Claude Flow
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ToolCategory {
    /// Agent lifecycle management
    AgentManagement,
    /// Memory storage and retrieval
    MemoryOperations,
    /// Multi-agent swarm coordination
    SwarmCoordination,
    /// Task creation and tracking
    TaskManagement,
    /// Hooks for learning and routing
    HooksLearning,
    /// Session state persistence
    SessionManagement,
    /// Workflow orchestration
    Workflow,
    /// System monitoring and health
    System,
    /// Configuration management
    Configuration,
    /// Hive-mind consensus
    HiveMind,
    /// Terminal operations
    Terminal,
    /// Neural/ML operations
    Neural,
    /// Performance monitoring
    Performance,
    /// GitHub integration
    GitHub,
    /// Claims/ownership
    Claims,
    /// AI security/defence
    AiDefence,
    /// Embeddings
    Embeddings,
    /// DAA (Decentralized Autonomous Agents)
    Daa,
    /// Coordination
    Coordination,
}

impl ToolCategory {
    /// Get all tool categories
    pub fn all() -> &'static [ToolCategory] {
        &[
            Self::AgentManagement,
            Self::MemoryOperations,
            Self::SwarmCoordination,
            Self::TaskManagement,
            Self::HooksLearning,
            Self::SessionManagement,
            Self::Workflow,
            Self::System,
            Self::Configuration,
            Self::HiveMind,
            Self::Terminal,
            Self::Neural,
            Self::Performance,
            Self::GitHub,
            Self::Claims,
            Self::AiDefence,
            Self::Embeddings,
            Self::Daa,
            Self::Coordination,
        ]
    }

    /// Get category name
    pub fn name(&self) -> &'static str {
        match self {
            Self::AgentManagement => "agent",
            Self::MemoryOperations => "memory",
            Self::SwarmCoordination => "swarm",
            Self::TaskManagement => "task",
            Self::HooksLearning => "hooks",
            Self::SessionManagement => "session",
            Self::Workflow => "workflow",
            Self::System => "system",
            Self::Configuration => "config",
            Self::HiveMind => "hive-mind",
            Self::Terminal => "terminal",
            Self::Neural => "neural",
            Self::Performance => "performance",
            Self::GitHub => "github",
            Self::Claims => "claims",
            Self::AiDefence => "aidefence",
            Self::Embeddings => "embeddings",
            Self::Daa => "daa",
            Self::Coordination => "coordination",
        }
    }
}

/// MCP Tool definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct McpToolDef {
    /// Tool name (e.g., "agent_spawn")
    pub name: String,
    /// Tool category
    pub category: ToolCategory,
    /// Description
    pub description: String,
    /// Required parameters
    pub required_params: Vec<ToolParam>,
    /// Optional parameters
    pub optional_params: Vec<ToolParam>,
    /// Example use cases
    pub use_cases: Vec<String>,
}

/// Tool parameter definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolParam {
    /// Parameter name
    pub name: String,
    /// Parameter type
    pub param_type: ParamType,
    /// Description
    pub description: String,
    /// Example values
    pub examples: Vec<String>,
}

/// Parameter types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ParamType {
    /// String value
    String,
    /// Integer value
    Integer,
    /// Boolean value
    Boolean,
    /// Float value
    Float,
    /// JSON object
    Object,
    /// Array of values
    Array,
    /// Enum (predefined values)
    Enum,
}

/// A single tool call training example
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolCallExample {
    /// Input prompt/request
    pub prompt: String,
    /// Expected tool to call
    pub expected_tool: String,
    /// Expected parameters
    pub expected_params: serde_json::Value,
    /// Whether this call succeeded
    pub success: bool,
    /// Category of the tool
    pub category: ToolCategory,
    /// Difficulty level
    pub difficulty: DifficultyLevel,
    /// Error message (if failure case)
    pub error_message: Option<String>,
    /// Alternative tools that could work
    pub alternatives: Vec<String>,
    /// Context about the scenario
    pub context: String,
    /// Quality score (0.0-1.0)
    pub quality_score: f32,
}

/// Difficulty levels for tool calling
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DifficultyLevel {
    /// Simple, single tool call
    Easy,
    /// Moderate complexity, may need parameter reasoning
    Medium,
    /// Complex scenario, multiple considerations
    Hard,
    /// Edge cases and error recovery
    Expert,
}

/// Configuration for tool dataset generation
#[derive(Debug, Clone)]
pub struct ToolDatasetConfig {
    /// Examples per tool
    pub examples_per_tool: usize,
    /// Include error/recovery cases
    pub include_error_cases: bool,
    /// Error case ratio (0.0-1.0)
    pub error_case_ratio: f32,
    /// Random seed
    pub seed: u64,
    /// Include multi-step scenarios
    pub include_multi_step: bool,
    /// Include alternative tools
    pub include_alternatives: bool,
    /// Difficulty distribution
    pub difficulty_weights: DifficultyWeights,
}

/// Weights for difficulty distribution
#[derive(Debug, Clone)]
pub struct DifficultyWeights {
    pub easy: f32,
    pub medium: f32,
    pub hard: f32,
    pub expert: f32,
}

impl Default for DifficultyWeights {
    fn default() -> Self {
        Self {
            easy: 0.3,
            medium: 0.4,
            hard: 0.2,
            expert: 0.1,
        }
    }
}

impl Default for ToolDatasetConfig {
    fn default() -> Self {
        Self {
            examples_per_tool: 10,
            include_error_cases: true,
            error_case_ratio: 0.15,
            seed: 42,
            include_multi_step: true,
            include_alternatives: true,
            difficulty_weights: DifficultyWeights::default(),
        }
    }
}

impl ToolDatasetConfig {
    /// Create config for comprehensive training
    pub fn comprehensive() -> Self {
        Self {
            examples_per_tool: 20,
            include_error_cases: true,
            error_case_ratio: 0.2,
            include_multi_step: true,
            include_alternatives: true,
            difficulty_weights: DifficultyWeights {
                easy: 0.25,
                medium: 0.35,
                hard: 0.25,
                expert: 0.15,
            },
            ..Default::default()
        }
    }

    /// Create config for quick testing
    pub fn minimal() -> Self {
        Self {
            examples_per_tool: 3,
            include_error_cases: false,
            error_case_ratio: 0.0,
            include_multi_step: false,
            include_alternatives: false,
            ..Default::default()
        }
    }
}

/// Dataset statistics
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ToolDatasetStats {
    /// Total examples
    pub total_examples: usize,
    /// Examples per category
    pub by_category: HashMap<String, usize>,
    /// Examples per tool
    pub by_tool: HashMap<String, usize>,
    /// Examples per difficulty
    pub by_difficulty: HashMap<String, usize>,
    /// Success/error ratio
    pub success_count: usize,
    /// Error examples count
    pub error_count: usize,
    /// Average quality score
    pub avg_quality: f32,
}

/// Complete tool calling dataset
#[derive(Debug)]
pub struct ToolCallDataset {
    /// All examples
    pub examples: Vec<ToolCallExample>,
    /// Tool definitions
    pub tool_definitions: Vec<McpToolDef>,
    /// Statistics
    pub stats: ToolDatasetStats,
}

impl ToolCallDataset {
    /// Generate a complete tool calling dataset
    pub fn generate(config: ToolDatasetConfig) -> Result<Self> {
        let mut generator = ToolDatasetGenerator::new(config);
        generator.generate()
    }

    /// Get the number of examples
    pub fn len(&self) -> usize {
        self.examples.len()
    }

    /// Check if dataset is empty
    pub fn is_empty(&self) -> bool {
        self.examples.is_empty()
    }

    /// Export to JSONL format
    pub fn export_jsonl<P: AsRef<Path>>(&self, path: P) -> std::io::Result<()> {
        let file = File::create(path)?;
        let mut writer = BufWriter::new(file);

        for example in &self.examples {
            let json = serde_json::to_string(example)?;
            writeln!(writer, "{}", json)?;
        }

        writer.flush()?;
        Ok(())
    }

    /// Export to JSON format
    pub fn export_json<P: AsRef<Path>>(&self, path: P) -> std::io::Result<()> {
        let file = File::create(path)?;
        serde_json::to_writer_pretty(file, &self.examples)?;
        Ok(())
    }

    /// Export tool definitions
    pub fn export_tool_defs<P: AsRef<Path>>(&self, path: P) -> std::io::Result<()> {
        let file = File::create(path)?;
        serde_json::to_writer_pretty(file, &self.tool_definitions)?;
        Ok(())
    }

    /// Filter examples by category
    pub fn filter_by_category(&self, category: ToolCategory) -> Vec<&ToolCallExample> {
        self.examples
            .iter()
            .filter(|e| e.category == category)
            .collect()
    }

    /// Filter examples by tool
    pub fn filter_by_tool(&self, tool_name: &str) -> Vec<&ToolCallExample> {
        self.examples
            .iter()
            .filter(|e| e.expected_tool == tool_name)
            .collect()
    }

    /// Filter examples by difficulty
    pub fn filter_by_difficulty(&self, difficulty: DifficultyLevel) -> Vec<&ToolCallExample> {
        self.examples
            .iter()
            .filter(|e| e.difficulty == difficulty)
            .collect()
    }

    /// Split into train/validation/test sets
    pub fn split(
        &self,
        train_ratio: f32,
        val_ratio: f32,
        seed: u64,
    ) -> (
        Vec<ToolCallExample>,
        Vec<ToolCallExample>,
        Vec<ToolCallExample>,
    ) {
        let mut rng = StdRng::seed_from_u64(seed);
        let mut examples = self.examples.clone();
        examples.shuffle(&mut rng);

        let n = examples.len();
        let train_end = (n as f32 * train_ratio) as usize;
        let val_end = train_end + (n as f32 * val_ratio) as usize;

        let train = examples[..train_end].to_vec();
        let val = examples[train_end..val_end].to_vec();
        let test = examples[val_end..].to_vec();

        (train, val, test)
    }
}

/// Dataset generator for tool calling examples
pub struct ToolDatasetGenerator {
    config: ToolDatasetConfig,
    rng: StdRng,
    tools: Vec<McpToolDef>,
}

impl ToolDatasetGenerator {
    /// Create a new generator
    pub fn new(config: ToolDatasetConfig) -> Self {
        let rng = StdRng::seed_from_u64(config.seed);
        let tools = Self::define_mcp_tools();

        Self { config, rng, tools }
    }

    /// Generate the complete dataset
    pub fn generate(&mut self) -> Result<ToolCallDataset> {
        let mut examples = Vec::new();

        for tool in &self.tools.clone() {
            let tool_examples = self.generate_tool_examples(tool);
            examples.extend(tool_examples);
        }

        // Shuffle examples
        examples.shuffle(&mut self.rng);

        // Compute statistics
        let stats = Self::compute_stats(&examples);

        Ok(ToolCallDataset {
            examples,
            tool_definitions: self.tools.clone(),
            stats,
        })
    }

    /// Generate examples for a single tool
    fn generate_tool_examples(&mut self, tool: &McpToolDef) -> Vec<ToolCallExample> {
        let mut examples = Vec::new();

        for i in 0..self.config.examples_per_tool {
            let is_error = self.config.include_error_cases
                && self.rng.gen::<f32>() < self.config.error_case_ratio;

            let difficulty = self.sample_difficulty();

            let example = if is_error {
                self.generate_error_example(tool, difficulty)
            } else {
                self.generate_success_example(tool, difficulty, i)
            };

            examples.push(example);
        }

        examples
    }

    /// Sample a difficulty level based on weights
    fn sample_difficulty(&mut self) -> DifficultyLevel {
        let w = &self.config.difficulty_weights;
        let r = self.rng.gen::<f32>();

        if r < w.easy {
            DifficultyLevel::Easy
        } else if r < w.easy + w.medium {
            DifficultyLevel::Medium
        } else if r < w.easy + w.medium + w.hard {
            DifficultyLevel::Hard
        } else {
            DifficultyLevel::Expert
        }
    }

    /// Generate a success example
    fn generate_success_example(
        &mut self,
        tool: &McpToolDef,
        difficulty: DifficultyLevel,
        index: usize,
    ) -> ToolCallExample {
        let prompt_template = self.get_prompt_template(tool, difficulty, index);
        let params = self.generate_params(tool, difficulty);

        let context = self.generate_context(tool, difficulty);
        let alternatives = if self.config.include_alternatives {
            self.get_alternative_tools(tool)
        } else {
            Vec::new()
        };

        let quality = match difficulty {
            DifficultyLevel::Easy => 0.95 + self.rng.gen::<f32>() * 0.05,
            DifficultyLevel::Medium => 0.85 + self.rng.gen::<f32>() * 0.10,
            DifficultyLevel::Hard => 0.75 + self.rng.gen::<f32>() * 0.15,
            DifficultyLevel::Expert => 0.70 + self.rng.gen::<f32>() * 0.20,
        };

        ToolCallExample {
            prompt: prompt_template,
            expected_tool: tool.name.clone(),
            expected_params: params,
            success: true,
            category: tool.category,
            difficulty,
            error_message: None,
            alternatives,
            context,
            quality_score: quality,
        }
    }

    /// Generate an error/recovery example
    fn generate_error_example(
        &mut self,
        tool: &McpToolDef,
        difficulty: DifficultyLevel,
    ) -> ToolCallExample {
        let error_types = [
            ("Missing required parameter", "Parameter validation failed"),
            ("Invalid parameter type", "Type mismatch error"),
            (
                "Resource not found",
                "The specified resource does not exist",
            ),
            ("Permission denied", "Insufficient permissions"),
            ("Rate limited", "Too many requests"),
        ];

        let (error_type, error_msg) = error_types.choose(&mut self.rng).unwrap();

        let prompt = format!(
            "Call {} but with incomplete or incorrect parameters for error handling training",
            tool.name
        );

        let mut params = self.generate_params(tool, difficulty);
        // Corrupt the params for error case
        if let Some(obj) = params.as_object_mut() {
            if !obj.is_empty() {
                let keys: Vec<String> = obj.keys().cloned().collect();
                if let Some(key) = keys.choose(&mut self.rng) {
                    obj.remove(key);
                }
            }
        }

        ToolCallExample {
            prompt,
            expected_tool: tool.name.clone(),
            expected_params: params,
            success: false,
            category: tool.category,
            difficulty,
            error_message: Some(format!("{}: {}", error_type, error_msg)),
            alternatives: Vec::new(),
            context: format!("Error recovery scenario for {}", tool.name),
            quality_score: 0.7,
        }
    }

    /// Get prompt template for a tool
    fn get_prompt_template(
        &mut self,
        tool: &McpToolDef,
        difficulty: DifficultyLevel,
        index: usize,
    ) -> String {
        let use_case = if !tool.use_cases.is_empty() {
            tool.use_cases[index % tool.use_cases.len()].clone()
        } else {
            tool.description.clone()
        };

        match difficulty {
            DifficultyLevel::Easy => format!("I need to {} using the {} tool", use_case, tool.name),
            DifficultyLevel::Medium => format!(
                "Help me {}. I want to use the appropriate MCP tool for this task.",
                use_case
            ),
            DifficultyLevel::Hard => format!(
                "I have a complex requirement: {}. Determine the best tool and parameters.",
                use_case
            ),
            DifficultyLevel::Expert => format!(
                "Given the scenario: {} - what tool should I use and how should I handle potential edge cases?",
                use_case
            ),
        }
    }

    /// Generate parameters for a tool
    fn generate_params(
        &mut self,
        tool: &McpToolDef,
        _difficulty: DifficultyLevel,
    ) -> serde_json::Value {
        let mut params = serde_json::Map::new();

        // Add required parameters
        for param in &tool.required_params {
            let value = self.generate_param_value(param);
            params.insert(param.name.clone(), value);
        }

        // Randomly add some optional parameters
        for param in &tool.optional_params {
            if self.rng.gen_bool(0.5) {
                let value = self.generate_param_value(param);
                params.insert(param.name.clone(), value);
            }
        }

        serde_json::Value::Object(params)
    }

    /// Generate a value for a parameter
    fn generate_param_value(&mut self, param: &ToolParam) -> serde_json::Value {
        if !param.examples.is_empty() && self.rng.gen_bool(0.7) {
            let example = param.examples.choose(&mut self.rng).unwrap();
            // Try to parse as appropriate type
            match param.param_type {
                ParamType::Integer => {
                    if let Ok(n) = example.parse::<i64>() {
                        return serde_json::Value::Number(n.into());
                    }
                }
                ParamType::Float => {
                    if let Ok(n) = example.parse::<f64>() {
                        if let Some(num) = serde_json::Number::from_f64(n) {
                            return serde_json::Value::Number(num);
                        }
                    }
                }
                ParamType::Boolean => {
                    if let Ok(b) = example.parse::<bool>() {
                        return serde_json::Value::Bool(b);
                    }
                }
                _ => {}
            }
            return serde_json::Value::String(example.clone());
        }

        match param.param_type {
            ParamType::String => {
                serde_json::Value::String(format!("example_{}", self.rng.gen::<u32>()))
            }
            ParamType::Integer => serde_json::Value::Number((self.rng.gen_range(1..100)).into()),
            ParamType::Boolean => serde_json::Value::Bool(self.rng.gen()),
            ParamType::Float => {
                let f = self.rng.gen::<f64>();
                serde_json::Number::from_f64(f)
                    .map(serde_json::Value::Number)
                    .unwrap_or(serde_json::Value::Number(0.into()))
            }
            ParamType::Object => serde_json::Value::Object(serde_json::Map::new()),
            ParamType::Array => serde_json::Value::Array(vec![]),
            ParamType::Enum => {
                if !param.examples.is_empty() {
                    serde_json::Value::String(param.examples.choose(&mut self.rng).unwrap().clone())
                } else {
                    serde_json::Value::String("default".to_string())
                }
            }
        }
    }

    /// Generate context for an example
    fn generate_context(&mut self, tool: &McpToolDef, difficulty: DifficultyLevel) -> String {
        let contexts = match difficulty {
            DifficultyLevel::Easy => vec![
                format!("Simple {} operation", tool.category.name()),
                format!("Basic use of {}", tool.name),
            ],
            DifficultyLevel::Medium => vec![
                format!("Standard {} workflow", tool.category.name()),
                format!("Common {} scenario", tool.name),
            ],
            DifficultyLevel::Hard => vec![
                format!("Complex {} integration", tool.category.name()),
                format!("Multi-step {} scenario", tool.name),
            ],
            DifficultyLevel::Expert => vec![
                format!("Edge case handling for {}", tool.name),
                format!(
                    "Production scenario with {} error handling",
                    tool.category.name()
                ),
            ],
        };

        contexts.choose(&mut self.rng).unwrap().clone()
    }

    /// Get alternative tools for a given tool
    fn get_alternative_tools(&self, tool: &McpToolDef) -> Vec<String> {
        self.tools
            .iter()
            .filter(|t| t.category == tool.category && t.name != tool.name)
            .take(2)
            .map(|t| t.name.clone())
            .collect()
    }

    /// Compute dataset statistics
    fn compute_stats(examples: &[ToolCallExample]) -> ToolDatasetStats {
        let mut stats = ToolDatasetStats {
            total_examples: examples.len(),
            ..Default::default()
        };

        let mut total_quality = 0.0f32;

        for example in examples {
            // By category
            *stats
                .by_category
                .entry(example.category.name().to_string())
                .or_insert(0) += 1;

            // By tool
            *stats
                .by_tool
                .entry(example.expected_tool.clone())
                .or_insert(0) += 1;

            // By difficulty
            *stats
                .by_difficulty
                .entry(format!("{:?}", example.difficulty))
                .or_insert(0) += 1;

            // Success/error
            if example.success {
                stats.success_count += 1;
            } else {
                stats.error_count += 1;
            }

            total_quality += example.quality_score;
        }

        if !examples.is_empty() {
            stats.avg_quality = total_quality / examples.len() as f32;
        }

        stats
    }

    /// Define all 140+ MCP tools
    fn define_mcp_tools() -> Vec<McpToolDef> {
        let mut tools = Vec::new();

        // ===== Agent Management Tools =====
        tools.push(McpToolDef {
            name: "agent_spawn".to_string(),
            category: ToolCategory::AgentManagement,
            description: "Spawn a new agent with intelligent model selection".to_string(),
            required_params: vec![ToolParam {
                name: "agentType".to_string(),
                param_type: ParamType::String,
                description: "Type of agent to spawn".to_string(),
                examples: vec![
                    "coder".to_string(),
                    "researcher".to_string(),
                    "tester".to_string(),
                    "reviewer".to_string(),
                ],
            }],
            optional_params: vec![
                ToolParam {
                    name: "agentId".to_string(),
                    param_type: ParamType::String,
                    description: "Custom agent ID".to_string(),
                    examples: vec!["agent-1".to_string(), "coder-main".to_string()],
                },
                ToolParam {
                    name: "model".to_string(),
                    param_type: ParamType::Enum,
                    description: "Claude model to use".to_string(),
                    examples: vec![
                        "haiku".to_string(),
                        "sonnet".to_string(),
                        "opus".to_string(),
                    ],
                },
                ToolParam {
                    name: "task".to_string(),
                    param_type: ParamType::String,
                    description: "Task description for model routing".to_string(),
                    examples: vec![
                        "implement authentication".to_string(),
                        "write tests".to_string(),
                    ],
                },
            ],
            use_cases: vec![
                "spawn a coder agent to implement a feature".to_string(),
                "create a researcher agent to analyze requirements".to_string(),
                "start a tester agent with opus model for complex testing".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "agent_terminate".to_string(),
            category: ToolCategory::AgentManagement,
            description: "Terminate an agent".to_string(),
            required_params: vec![ToolParam {
                name: "agentId".to_string(),
                param_type: ParamType::String,
                description: "ID of agent to terminate".to_string(),
                examples: vec!["agent-1".to_string(), "coder-main".to_string()],
            }],
            optional_params: vec![ToolParam {
                name: "force".to_string(),
                param_type: ParamType::Boolean,
                description: "Force immediate termination".to_string(),
                examples: vec!["true".to_string(), "false".to_string()],
            }],
            use_cases: vec![
                "stop an agent that has completed its task".to_string(),
                "force terminate an unresponsive agent".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "agent_status".to_string(),
            category: ToolCategory::AgentManagement,
            description: "Get agent status".to_string(),
            required_params: vec![ToolParam {
                name: "agentId".to_string(),
                param_type: ParamType::String,
                description: "ID of agent".to_string(),
                examples: vec!["agent-1".to_string()],
            }],
            optional_params: vec![],
            use_cases: vec![
                "check if an agent is still running".to_string(),
                "get current status of a specific agent".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "agent_list".to_string(),
            category: ToolCategory::AgentManagement,
            description: "List all agents".to_string(),
            required_params: vec![],
            optional_params: vec![
                ToolParam {
                    name: "status".to_string(),
                    param_type: ParamType::String,
                    description: "Filter by status".to_string(),
                    examples: vec![
                        "running".to_string(),
                        "idle".to_string(),
                        "terminated".to_string(),
                    ],
                },
                ToolParam {
                    name: "includeTerminated".to_string(),
                    param_type: ParamType::Boolean,
                    description: "Include terminated agents".to_string(),
                    examples: vec!["true".to_string(), "false".to_string()],
                },
            ],
            use_cases: vec![
                "list all currently running agents".to_string(),
                "get a full inventory of agents including terminated ones".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "agent_pool".to_string(),
            category: ToolCategory::AgentManagement,
            description: "Manage agent pool".to_string(),
            required_params: vec![ToolParam {
                name: "action".to_string(),
                param_type: ParamType::Enum,
                description: "Pool action".to_string(),
                examples: vec![
                    "status".to_string(),
                    "scale".to_string(),
                    "drain".to_string(),
                    "fill".to_string(),
                ],
            }],
            optional_params: vec![ToolParam {
                name: "targetSize".to_string(),
                param_type: ParamType::Integer,
                description: "Target pool size".to_string(),
                examples: vec!["5".to_string(), "10".to_string()],
            }],
            use_cases: vec![
                "scale the agent pool to handle increased load".to_string(),
                "drain the pool before maintenance".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "agent_health".to_string(),
            category: ToolCategory::AgentManagement,
            description: "Check agent health".to_string(),
            required_params: vec![],
            optional_params: vec![
                ToolParam {
                    name: "agentId".to_string(),
                    param_type: ParamType::String,
                    description: "Specific agent ID".to_string(),
                    examples: vec!["agent-1".to_string()],
                },
                ToolParam {
                    name: "threshold".to_string(),
                    param_type: ParamType::Float,
                    description: "Health threshold".to_string(),
                    examples: vec!["0.8".to_string(), "0.9".to_string()],
                },
            ],
            use_cases: vec![
                "check health of all agents in the swarm".to_string(),
                "verify a specific agent meets health threshold".to_string(),
            ],
        });

        // ===== Memory Operations Tools =====
        tools.push(McpToolDef {
            name: "memory_store".to_string(),
            category: ToolCategory::MemoryOperations,
            description: "Store a value in memory (persisted to disk)".to_string(),
            required_params: vec![
                ToolParam {
                    name: "key".to_string(),
                    param_type: ParamType::String,
                    description: "Memory key".to_string(),
                    examples: vec!["user-prefs".to_string(), "session-state".to_string()],
                },
                ToolParam {
                    name: "value".to_string(),
                    param_type: ParamType::Object,
                    description: "Value to store".to_string(),
                    examples: vec!["{}".to_string()],
                },
            ],
            optional_params: vec![ToolParam {
                name: "metadata".to_string(),
                param_type: ParamType::Object,
                description: "Optional metadata".to_string(),
                examples: vec!["{}".to_string()],
            }],
            use_cases: vec![
                "store user preferences for later retrieval".to_string(),
                "persist session state across conversations".to_string(),
                "save learned patterns for the intelligence system".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "memory_retrieve".to_string(),
            category: ToolCategory::MemoryOperations,
            description: "Retrieve a value from memory".to_string(),
            required_params: vec![ToolParam {
                name: "key".to_string(),
                param_type: ParamType::String,
                description: "Memory key".to_string(),
                examples: vec!["user-prefs".to_string()],
            }],
            optional_params: vec![],
            use_cases: vec![
                "get previously stored user preferences".to_string(),
                "retrieve session state from last conversation".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "memory_search".to_string(),
            category: ToolCategory::MemoryOperations,
            description: "Search memory by keyword".to_string(),
            required_params: vec![ToolParam {
                name: "query".to_string(),
                param_type: ParamType::String,
                description: "Search query".to_string(),
                examples: vec!["authentication".to_string(), "user settings".to_string()],
            }],
            optional_params: vec![ToolParam {
                name: "limit".to_string(),
                param_type: ParamType::Integer,
                description: "Result limit".to_string(),
                examples: vec!["10".to_string(), "50".to_string()],
            }],
            use_cases: vec![
                "search for entries related to authentication".to_string(),
                "find all memory entries matching a pattern".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "memory_delete".to_string(),
            category: ToolCategory::MemoryOperations,
            description: "Delete a memory entry".to_string(),
            required_params: vec![ToolParam {
                name: "key".to_string(),
                param_type: ParamType::String,
                description: "Memory key".to_string(),
                examples: vec!["old-session".to_string()],
            }],
            optional_params: vec![],
            use_cases: vec![
                "remove outdated session data".to_string(),
                "clean up temporary memory entries".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "memory_list".to_string(),
            category: ToolCategory::MemoryOperations,
            description: "List all memory entries".to_string(),
            required_params: vec![],
            optional_params: vec![
                ToolParam {
                    name: "limit".to_string(),
                    param_type: ParamType::Integer,
                    description: "Result limit".to_string(),
                    examples: vec!["100".to_string()],
                },
                ToolParam {
                    name: "offset".to_string(),
                    param_type: ParamType::Integer,
                    description: "Result offset".to_string(),
                    examples: vec!["0".to_string()],
                },
            ],
            use_cases: vec![
                "list all stored memory entries".to_string(),
                "paginate through memory entries".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "memory_stats".to_string(),
            category: ToolCategory::MemoryOperations,
            description: "Get memory storage statistics".to_string(),
            required_params: vec![],
            optional_params: vec![],
            use_cases: vec![
                "check memory usage statistics".to_string(),
                "monitor memory storage capacity".to_string(),
            ],
        });

        // ===== Swarm Coordination Tools =====
        tools.push(McpToolDef {
            name: "swarm_init".to_string(),
            category: ToolCategory::SwarmCoordination,
            description: "Initialize a swarm".to_string(),
            required_params: vec![],
            optional_params: vec![
                ToolParam {
                    name: "topology".to_string(),
                    param_type: ParamType::Enum,
                    description: "Swarm topology type".to_string(),
                    examples: vec![
                        "hierarchical".to_string(),
                        "mesh".to_string(),
                        "star".to_string(),
                    ],
                },
                ToolParam {
                    name: "maxAgents".to_string(),
                    param_type: ParamType::Integer,
                    description: "Maximum number of agents".to_string(),
                    examples: vec!["8".to_string(), "15".to_string()],
                },
            ],
            use_cases: vec![
                "initialize a hierarchical swarm for coordinated work".to_string(),
                "set up a mesh topology for peer-to-peer coordination".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "swarm_status".to_string(),
            category: ToolCategory::SwarmCoordination,
            description: "Get swarm status".to_string(),
            required_params: vec![],
            optional_params: vec![ToolParam {
                name: "swarmId".to_string(),
                param_type: ParamType::String,
                description: "Swarm ID".to_string(),
                examples: vec!["swarm-1".to_string()],
            }],
            use_cases: vec![
                "check the current status of the swarm".to_string(),
                "monitor swarm health and agent count".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "swarm_shutdown".to_string(),
            category: ToolCategory::SwarmCoordination,
            description: "Shutdown a swarm".to_string(),
            required_params: vec![],
            optional_params: vec![
                ToolParam {
                    name: "swarmId".to_string(),
                    param_type: ParamType::String,
                    description: "Swarm ID".to_string(),
                    examples: vec!["swarm-1".to_string()],
                },
                ToolParam {
                    name: "graceful".to_string(),
                    param_type: ParamType::Boolean,
                    description: "Graceful shutdown".to_string(),
                    examples: vec!["true".to_string()],
                },
            ],
            use_cases: vec![
                "gracefully shutdown the swarm after completing tasks".to_string(),
                "force shutdown a problematic swarm".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "swarm_health".to_string(),
            category: ToolCategory::SwarmCoordination,
            description: "Check swarm health status".to_string(),
            required_params: vec![],
            optional_params: vec![ToolParam {
                name: "swarmId".to_string(),
                param_type: ParamType::String,
                description: "Swarm ID to check".to_string(),
                examples: vec!["swarm-1".to_string()],
            }],
            use_cases: vec![
                "verify swarm is healthy before assigning tasks".to_string(),
                "diagnose issues in a malfunctioning swarm".to_string(),
            ],
        });

        // ===== Task Management Tools =====
        tools.push(McpToolDef {
            name: "task_create".to_string(),
            category: ToolCategory::TaskManagement,
            description: "Create a new task".to_string(),
            required_params: vec![
                ToolParam {
                    name: "type".to_string(),
                    param_type: ParamType::Enum,
                    description: "Task type".to_string(),
                    examples: vec![
                        "feature".to_string(),
                        "bugfix".to_string(),
                        "research".to_string(),
                    ],
                },
                ToolParam {
                    name: "description".to_string(),
                    param_type: ParamType::String,
                    description: "Task description".to_string(),
                    examples: vec!["Implement user authentication".to_string()],
                },
            ],
            optional_params: vec![
                ToolParam {
                    name: "priority".to_string(),
                    param_type: ParamType::Enum,
                    description: "Task priority".to_string(),
                    examples: vec![
                        "low".to_string(),
                        "normal".to_string(),
                        "high".to_string(),
                        "critical".to_string(),
                    ],
                },
                ToolParam {
                    name: "assignTo".to_string(),
                    param_type: ParamType::Array,
                    description: "Agent IDs to assign".to_string(),
                    examples: vec!["[\"agent-1\"]".to_string()],
                },
            ],
            use_cases: vec![
                "create a feature task and assign it to a coder".to_string(),
                "create a high-priority bugfix task".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "task_status".to_string(),
            category: ToolCategory::TaskManagement,
            description: "Get task status".to_string(),
            required_params: vec![ToolParam {
                name: "taskId".to_string(),
                param_type: ParamType::String,
                description: "Task ID".to_string(),
                examples: vec!["task-123".to_string()],
            }],
            optional_params: vec![],
            use_cases: vec![
                "check progress of a specific task".to_string(),
                "verify if a task has been completed".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "task_list".to_string(),
            category: ToolCategory::TaskManagement,
            description: "List all tasks".to_string(),
            required_params: vec![],
            optional_params: vec![
                ToolParam {
                    name: "status".to_string(),
                    param_type: ParamType::String,
                    description: "Filter by status".to_string(),
                    examples: vec![
                        "pending".to_string(),
                        "in_progress".to_string(),
                        "completed".to_string(),
                    ],
                },
                ToolParam {
                    name: "priority".to_string(),
                    param_type: ParamType::String,
                    description: "Filter by priority".to_string(),
                    examples: vec!["high".to_string(), "critical".to_string()],
                },
            ],
            use_cases: vec![
                "list all pending tasks".to_string(),
                "get all high-priority tasks in progress".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "task_complete".to_string(),
            category: ToolCategory::TaskManagement,
            description: "Mark task as complete".to_string(),
            required_params: vec![ToolParam {
                name: "taskId".to_string(),
                param_type: ParamType::String,
                description: "Task ID".to_string(),
                examples: vec!["task-123".to_string()],
            }],
            optional_params: vec![ToolParam {
                name: "result".to_string(),
                param_type: ParamType::Object,
                description: "Task result data".to_string(),
                examples: vec!["{}".to_string()],
            }],
            use_cases: vec![
                "mark a task as completed with results".to_string(),
                "finalize a task after review".to_string(),
            ],
        });

        // ===== Hooks & Learning Tools =====
        tools.push(McpToolDef {
            name: "hooks_pre-task".to_string(),
            category: ToolCategory::HooksLearning,
            description:
                "Record task start and get agent suggestions with intelligent model routing"
                    .to_string(),
            required_params: vec![
                ToolParam {
                    name: "taskId".to_string(),
                    param_type: ParamType::String,
                    description: "Task identifier".to_string(),
                    examples: vec!["task-001".to_string()],
                },
                ToolParam {
                    name: "description".to_string(),
                    param_type: ParamType::String,
                    description: "Task description".to_string(),
                    examples: vec!["Implement user login".to_string()],
                },
            ],
            optional_params: vec![ToolParam {
                name: "filePath".to_string(),
                param_type: ParamType::String,
                description: "Optional file path for AST analysis".to_string(),
                examples: vec!["src/auth.rs".to_string()],
            }],
            use_cases: vec![
                "get agent routing suggestions before starting a task".to_string(),
                "record task start for learning system".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "hooks_post-task".to_string(),
            category: ToolCategory::HooksLearning,
            description: "Record task completion for learning".to_string(),
            required_params: vec![ToolParam {
                name: "taskId".to_string(),
                param_type: ParamType::String,
                description: "Task identifier".to_string(),
                examples: vec!["task-001".to_string()],
            }],
            optional_params: vec![
                ToolParam {
                    name: "success".to_string(),
                    param_type: ParamType::Boolean,
                    description: "Whether task was successful".to_string(),
                    examples: vec!["true".to_string()],
                },
                ToolParam {
                    name: "quality".to_string(),
                    param_type: ParamType::Float,
                    description: "Quality score (0-1)".to_string(),
                    examples: vec!["0.9".to_string()],
                },
            ],
            use_cases: vec![
                "record successful task completion for reinforcement learning".to_string(),
                "provide feedback on task quality".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "hooks_route".to_string(),
            category: ToolCategory::HooksLearning,
            description: "Route task to optimal agent using learned patterns".to_string(),
            required_params: vec![ToolParam {
                name: "task".to_string(),
                param_type: ParamType::String,
                description: "Task description".to_string(),
                examples: vec!["implement caching layer".to_string()],
            }],
            optional_params: vec![ToolParam {
                name: "context".to_string(),
                param_type: ParamType::String,
                description: "Additional context".to_string(),
                examples: vec!["performance-critical".to_string()],
            }],
            use_cases: vec![
                "get the optimal agent type for a given task".to_string(),
                "use learned patterns to route tasks intelligently".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "hooks_metrics".to_string(),
            category: ToolCategory::HooksLearning,
            description: "View learning metrics dashboard".to_string(),
            required_params: vec![],
            optional_params: vec![
                ToolParam {
                    name: "period".to_string(),
                    param_type: ParamType::Enum,
                    description: "Metrics period".to_string(),
                    examples: vec!["1h".to_string(), "24h".to_string(), "7d".to_string()],
                },
                ToolParam {
                    name: "includeV3".to_string(),
                    param_type: ParamType::Boolean,
                    description: "Include V3 performance metrics".to_string(),
                    examples: vec!["true".to_string()],
                },
            ],
            use_cases: vec![
                "view learning system performance metrics".to_string(),
                "analyze agent routing effectiveness".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "hooks_pre-edit".to_string(),
            category: ToolCategory::HooksLearning,
            description: "Get context and agent suggestions before editing a file".to_string(),
            required_params: vec![ToolParam {
                name: "filePath".to_string(),
                param_type: ParamType::String,
                description: "Path to the file being edited".to_string(),
                examples: vec!["src/main.rs".to_string()],
            }],
            optional_params: vec![ToolParam {
                name: "operation".to_string(),
                param_type: ParamType::Enum,
                description: "Type of operation".to_string(),
                examples: vec![
                    "create".to_string(),
                    "update".to_string(),
                    "refactor".to_string(),
                ],
            }],
            use_cases: vec![
                "get suggestions before editing a source file".to_string(),
                "analyze file context for intelligent assistance".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "hooks_post-edit".to_string(),
            category: ToolCategory::HooksLearning,
            description: "Record editing outcome for learning".to_string(),
            required_params: vec![ToolParam {
                name: "filePath".to_string(),
                param_type: ParamType::String,
                description: "Path to the edited file".to_string(),
                examples: vec!["src/main.rs".to_string()],
            }],
            optional_params: vec![ToolParam {
                name: "success".to_string(),
                param_type: ParamType::Boolean,
                description: "Whether the edit was successful".to_string(),
                examples: vec!["true".to_string()],
            }],
            use_cases: vec![
                "record successful edit for learning".to_string(),
                "track edit outcomes for pattern learning".to_string(),
            ],
        });

        // ===== Session Management Tools =====
        tools.push(McpToolDef {
            name: "session_save".to_string(),
            category: ToolCategory::SessionManagement,
            description: "Save current session state".to_string(),
            required_params: vec![ToolParam {
                name: "name".to_string(),
                param_type: ParamType::String,
                description: "Session name".to_string(),
                examples: vec!["feature-auth".to_string()],
            }],
            optional_params: vec![
                ToolParam {
                    name: "includeAgents".to_string(),
                    param_type: ParamType::Boolean,
                    description: "Include agents in session".to_string(),
                    examples: vec!["true".to_string()],
                },
                ToolParam {
                    name: "includeMemory".to_string(),
                    param_type: ParamType::Boolean,
                    description: "Include memory in session".to_string(),
                    examples: vec!["true".to_string()],
                },
            ],
            use_cases: vec![
                "save current work session before break".to_string(),
                "persist session state for later continuation".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "session_restore".to_string(),
            category: ToolCategory::SessionManagement,
            description: "Restore a saved session".to_string(),
            required_params: vec![],
            optional_params: vec![
                ToolParam {
                    name: "name".to_string(),
                    param_type: ParamType::String,
                    description: "Session name to restore".to_string(),
                    examples: vec!["feature-auth".to_string()],
                },
                ToolParam {
                    name: "sessionId".to_string(),
                    param_type: ParamType::String,
                    description: "Session ID to restore".to_string(),
                    examples: vec!["session-123".to_string()],
                },
            ],
            use_cases: vec![
                "restore a previously saved session".to_string(),
                "continue work from a saved checkpoint".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "session_list".to_string(),
            category: ToolCategory::SessionManagement,
            description: "List saved sessions".to_string(),
            required_params: vec![],
            optional_params: vec![ToolParam {
                name: "limit".to_string(),
                param_type: ParamType::Integer,
                description: "Maximum sessions to return".to_string(),
                examples: vec!["10".to_string()],
            }],
            use_cases: vec![
                "view all saved sessions".to_string(),
                "find a specific session to restore".to_string(),
            ],
        });

        // ===== Workflow Tools =====
        tools.push(McpToolDef {
            name: "workflow_create".to_string(),
            category: ToolCategory::Workflow,
            description: "Create a new workflow".to_string(),
            required_params: vec![ToolParam {
                name: "name".to_string(),
                param_type: ParamType::String,
                description: "Workflow name".to_string(),
                examples: vec!["feature-development".to_string()],
            }],
            optional_params: vec![
                ToolParam {
                    name: "steps".to_string(),
                    param_type: ParamType::Array,
                    description: "Workflow steps".to_string(),
                    examples: vec!["[]".to_string()],
                },
                ToolParam {
                    name: "description".to_string(),
                    param_type: ParamType::String,
                    description: "Workflow description".to_string(),
                    examples: vec!["Full feature development workflow".to_string()],
                },
            ],
            use_cases: vec![
                "create a multi-step development workflow".to_string(),
                "define a reusable workflow template".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "workflow_execute".to_string(),
            category: ToolCategory::Workflow,
            description: "Execute a workflow".to_string(),
            required_params: vec![ToolParam {
                name: "workflowId".to_string(),
                param_type: ParamType::String,
                description: "Workflow ID to execute".to_string(),
                examples: vec!["workflow-123".to_string()],
            }],
            optional_params: vec![ToolParam {
                name: "variables".to_string(),
                param_type: ParamType::Object,
                description: "Runtime variables to inject".to_string(),
                examples: vec!["{}".to_string()],
            }],
            use_cases: vec![
                "execute a predefined workflow".to_string(),
                "run a workflow with custom variables".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "workflow_status".to_string(),
            category: ToolCategory::Workflow,
            description: "Get workflow status".to_string(),
            required_params: vec![ToolParam {
                name: "workflowId".to_string(),
                param_type: ParamType::String,
                description: "Workflow ID".to_string(),
                examples: vec!["workflow-123".to_string()],
            }],
            optional_params: vec![ToolParam {
                name: "verbose".to_string(),
                param_type: ParamType::Boolean,
                description: "Include step details".to_string(),
                examples: vec!["true".to_string()],
            }],
            use_cases: vec![
                "check progress of a running workflow".to_string(),
                "get detailed status including step information".to_string(),
            ],
        });

        // ===== System Tools =====
        tools.push(McpToolDef {
            name: "system_status".to_string(),
            category: ToolCategory::System,
            description: "Get overall system status".to_string(),
            required_params: vec![],
            optional_params: vec![ToolParam {
                name: "verbose".to_string(),
                param_type: ParamType::Boolean,
                description: "Include detailed information".to_string(),
                examples: vec!["true".to_string()],
            }],
            use_cases: vec![
                "check system health and status".to_string(),
                "get detailed system diagnostics".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "system_metrics".to_string(),
            category: ToolCategory::System,
            description: "Get system metrics and performance data".to_string(),
            required_params: vec![],
            optional_params: vec![
                ToolParam {
                    name: "category".to_string(),
                    param_type: ParamType::Enum,
                    description: "Metrics category".to_string(),
                    examples: vec!["all".to_string(), "cpu".to_string(), "memory".to_string()],
                },
                ToolParam {
                    name: "timeRange".to_string(),
                    param_type: ParamType::String,
                    description: "Time range".to_string(),
                    examples: vec!["1h".to_string(), "24h".to_string()],
                },
            ],
            use_cases: vec![
                "get CPU and memory metrics".to_string(),
                "analyze system performance over time".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "system_health".to_string(),
            category: ToolCategory::System,
            description: "Perform system health check".to_string(),
            required_params: vec![],
            optional_params: vec![
                ToolParam {
                    name: "deep".to_string(),
                    param_type: ParamType::Boolean,
                    description: "Perform deep health check".to_string(),
                    examples: vec!["true".to_string()],
                },
                ToolParam {
                    name: "fix".to_string(),
                    param_type: ParamType::Boolean,
                    description: "Attempt to fix issues".to_string(),
                    examples: vec!["true".to_string()],
                },
            ],
            use_cases: vec![
                "run a comprehensive health check".to_string(),
                "diagnose and fix system issues".to_string(),
            ],
        });

        // ===== Configuration Tools =====
        tools.push(McpToolDef {
            name: "config_get".to_string(),
            category: ToolCategory::Configuration,
            description: "Get configuration value".to_string(),
            required_params: vec![ToolParam {
                name: "key".to_string(),
                param_type: ParamType::String,
                description: "Configuration key (dot notation supported)".to_string(),
                examples: vec!["swarm.topology".to_string(), "memory.backend".to_string()],
            }],
            optional_params: vec![ToolParam {
                name: "scope".to_string(),
                param_type: ParamType::Enum,
                description: "Configuration scope".to_string(),
                examples: vec![
                    "project".to_string(),
                    "user".to_string(),
                    "system".to_string(),
                ],
            }],
            use_cases: vec![
                "get a specific configuration value".to_string(),
                "check swarm topology setting".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "config_set".to_string(),
            category: ToolCategory::Configuration,
            description: "Set configuration value".to_string(),
            required_params: vec![
                ToolParam {
                    name: "key".to_string(),
                    param_type: ParamType::String,
                    description: "Configuration key".to_string(),
                    examples: vec!["swarm.maxAgents".to_string()],
                },
                ToolParam {
                    name: "value".to_string(),
                    param_type: ParamType::Object,
                    description: "Configuration value".to_string(),
                    examples: vec!["10".to_string()],
                },
            ],
            optional_params: vec![ToolParam {
                name: "scope".to_string(),
                param_type: ParamType::String,
                description: "Configuration scope".to_string(),
                examples: vec!["project".to_string()],
            }],
            use_cases: vec![
                "update swarm configuration".to_string(),
                "change memory backend setting".to_string(),
            ],
        });

        // ===== Hive-Mind Tools =====
        tools.push(McpToolDef {
            name: "hive-mind_init".to_string(),
            category: ToolCategory::HiveMind,
            description: "Initialize the hive-mind collective".to_string(),
            required_params: vec![],
            optional_params: vec![
                ToolParam {
                    name: "topology".to_string(),
                    param_type: ParamType::Enum,
                    description: "Network topology".to_string(),
                    examples: vec![
                        "mesh".to_string(),
                        "hierarchical".to_string(),
                        "ring".to_string(),
                    ],
                },
                ToolParam {
                    name: "queenId".to_string(),
                    param_type: ParamType::String,
                    description: "Initial queen agent ID".to_string(),
                    examples: vec!["queen-1".to_string()],
                },
            ],
            use_cases: vec![
                "initialize a mesh-based hive-mind".to_string(),
                "set up hierarchical coordination with a queen".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "hive-mind_status".to_string(),
            category: ToolCategory::HiveMind,
            description: "Get hive-mind status".to_string(),
            required_params: vec![],
            optional_params: vec![ToolParam {
                name: "verbose".to_string(),
                param_type: ParamType::Boolean,
                description: "Include detailed information".to_string(),
                examples: vec!["true".to_string()],
            }],
            use_cases: vec![
                "check hive-mind collective status".to_string(),
                "monitor consensus state".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "hive-mind_consensus".to_string(),
            category: ToolCategory::HiveMind,
            description: "Propose or vote on consensus".to_string(),
            required_params: vec![ToolParam {
                name: "action".to_string(),
                param_type: ParamType::Enum,
                description: "Consensus action".to_string(),
                examples: vec![
                    "propose".to_string(),
                    "vote".to_string(),
                    "status".to_string(),
                ],
            }],
            optional_params: vec![
                ToolParam {
                    name: "proposalId".to_string(),
                    param_type: ParamType::String,
                    description: "Proposal ID".to_string(),
                    examples: vec!["proposal-1".to_string()],
                },
                ToolParam {
                    name: "vote".to_string(),
                    param_type: ParamType::Boolean,
                    description: "Vote (true=for, false=against)".to_string(),
                    examples: vec!["true".to_string()],
                },
            ],
            use_cases: vec![
                "propose a new decision for consensus".to_string(),
                "vote on an existing proposal".to_string(),
            ],
        });

        // ===== Neural Tools =====
        tools.push(McpToolDef {
            name: "neural_train".to_string(),
            category: ToolCategory::Neural,
            description: "Train a neural model".to_string(),
            required_params: vec![ToolParam {
                name: "modelType".to_string(),
                param_type: ParamType::Enum,
                description: "Model type".to_string(),
                examples: vec![
                    "moe".to_string(),
                    "transformer".to_string(),
                    "classifier".to_string(),
                ],
            }],
            optional_params: vec![
                ToolParam {
                    name: "epochs".to_string(),
                    param_type: ParamType::Integer,
                    description: "Number of training epochs".to_string(),
                    examples: vec!["10".to_string()],
                },
                ToolParam {
                    name: "learningRate".to_string(),
                    param_type: ParamType::Float,
                    description: "Learning rate".to_string(),
                    examples: vec!["0.001".to_string()],
                },
            ],
            use_cases: vec![
                "train a mixture of experts model".to_string(),
                "fine-tune classifier for task routing".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "neural_predict".to_string(),
            category: ToolCategory::Neural,
            description: "Make predictions using a neural model".to_string(),
            required_params: vec![ToolParam {
                name: "input".to_string(),
                param_type: ParamType::String,
                description: "Input text or data".to_string(),
                examples: vec!["implement user authentication".to_string()],
            }],
            optional_params: vec![
                ToolParam {
                    name: "modelId".to_string(),
                    param_type: ParamType::String,
                    description: "Model ID to use".to_string(),
                    examples: vec!["model-1".to_string()],
                },
                ToolParam {
                    name: "topK".to_string(),
                    param_type: ParamType::Integer,
                    description: "Number of top predictions".to_string(),
                    examples: vec!["5".to_string()],
                },
            ],
            use_cases: vec![
                "get neural model prediction for task routing".to_string(),
                "classify task complexity using neural model".to_string(),
            ],
        });

        // ===== Performance Tools =====
        tools.push(McpToolDef {
            name: "performance_report".to_string(),
            category: ToolCategory::Performance,
            description: "Generate performance report".to_string(),
            required_params: vec![],
            optional_params: vec![
                ToolParam {
                    name: "format".to_string(),
                    param_type: ParamType::Enum,
                    description: "Report format".to_string(),
                    examples: vec![
                        "json".to_string(),
                        "summary".to_string(),
                        "detailed".to_string(),
                    ],
                },
                ToolParam {
                    name: "timeRange".to_string(),
                    param_type: ParamType::String,
                    description: "Time range".to_string(),
                    examples: vec!["1h".to_string(), "24h".to_string()],
                },
            ],
            use_cases: vec![
                "generate a performance report for the last hour".to_string(),
                "get detailed performance analytics".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "performance_benchmark".to_string(),
            category: ToolCategory::Performance,
            description: "Run performance benchmarks".to_string(),
            required_params: vec![],
            optional_params: vec![
                ToolParam {
                    name: "suite".to_string(),
                    param_type: ParamType::Enum,
                    description: "Benchmark suite".to_string(),
                    examples: vec![
                        "all".to_string(),
                        "memory".to_string(),
                        "neural".to_string(),
                    ],
                },
                ToolParam {
                    name: "iterations".to_string(),
                    param_type: ParamType::Integer,
                    description: "Number of iterations".to_string(),
                    examples: vec!["100".to_string()],
                },
            ],
            use_cases: vec![
                "run comprehensive benchmarks".to_string(),
                "benchmark memory subsystem performance".to_string(),
            ],
        });

        // ===== AIDefence Tools =====
        tools.push(McpToolDef {
            name: "aidefence_scan".to_string(),
            category: ToolCategory::AiDefence,
            description: "Scan input text for AI manipulation threats".to_string(),
            required_params: vec![ToolParam {
                name: "input".to_string(),
                param_type: ParamType::String,
                description: "Text to scan for threats".to_string(),
                examples: vec!["user input text".to_string()],
            }],
            optional_params: vec![ToolParam {
                name: "quick".to_string(),
                param_type: ParamType::Boolean,
                description: "Quick scan mode".to_string(),
                examples: vec!["true".to_string()],
            }],
            use_cases: vec![
                "scan user input for prompt injection attempts".to_string(),
                "detect potential jailbreak attempts".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "aidefence_is_safe".to_string(),
            category: ToolCategory::AiDefence,
            description: "Quick boolean check if input is safe".to_string(),
            required_params: vec![ToolParam {
                name: "input".to_string(),
                param_type: ParamType::String,
                description: "Text to check".to_string(),
                examples: vec!["user message".to_string()],
            }],
            optional_params: vec![],
            use_cases: vec![
                "quickly validate user input is safe".to_string(),
                "guard against malicious inputs".to_string(),
            ],
        });

        // ===== Embeddings Tools =====
        tools.push(McpToolDef {
            name: "embeddings_generate".to_string(),
            category: ToolCategory::Embeddings,
            description: "Generate embeddings for text".to_string(),
            required_params: vec![ToolParam {
                name: "text".to_string(),
                param_type: ParamType::String,
                description: "Text to embed".to_string(),
                examples: vec!["implement authentication".to_string()],
            }],
            optional_params: vec![
                ToolParam {
                    name: "hyperbolic".to_string(),
                    param_type: ParamType::Boolean,
                    description: "Return hyperbolic embedding".to_string(),
                    examples: vec!["false".to_string()],
                },
                ToolParam {
                    name: "normalize".to_string(),
                    param_type: ParamType::Boolean,
                    description: "L2 normalize the embedding".to_string(),
                    examples: vec!["true".to_string()],
                },
            ],
            use_cases: vec![
                "generate embeddings for semantic search".to_string(),
                "create hyperbolic embeddings for hierarchical data".to_string(),
            ],
        });

        tools.push(McpToolDef {
            name: "embeddings_search".to_string(),
            category: ToolCategory::Embeddings,
            description: "Semantic search across stored embeddings".to_string(),
            required_params: vec![ToolParam {
                name: "query".to_string(),
                param_type: ParamType::String,
                description: "Search query".to_string(),
                examples: vec!["authentication patterns".to_string()],
            }],
            optional_params: vec![
                ToolParam {
                    name: "topK".to_string(),
                    param_type: ParamType::Integer,
                    description: "Number of results".to_string(),
                    examples: vec!["5".to_string()],
                },
                ToolParam {
                    name: "threshold".to_string(),
                    param_type: ParamType::Float,
                    description: "Minimum similarity threshold".to_string(),
                    examples: vec!["0.5".to_string()],
                },
            ],
            use_cases: vec![
                "find similar patterns using semantic search".to_string(),
                "retrieve relevant documents by meaning".to_string(),
            ],
        });

        tools
    }
}

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

    #[test]
    fn test_dataset_generation() {
        let config = ToolDatasetConfig {
            examples_per_tool: 3,
            include_error_cases: false,
            ..Default::default()
        };

        let dataset = ToolCallDataset::generate(config).unwrap();

        // Should have examples for all defined tools
        assert!(!dataset.examples.is_empty());
        assert!(!dataset.tool_definitions.is_empty());
    }

    #[test]
    fn test_tool_categories() {
        let categories = ToolCategory::all();
        assert!(categories.len() >= 10); // We have at least 10 categories
    }

    #[test]
    fn test_error_cases() {
        let config = ToolDatasetConfig {
            examples_per_tool: 10,
            include_error_cases: true,
            error_case_ratio: 0.5, // 50% error cases
            ..Default::default()
        };

        let dataset = ToolCallDataset::generate(config).unwrap();

        // Should have both success and error cases
        assert!(dataset.stats.success_count > 0);
        assert!(dataset.stats.error_count > 0);
    }

    #[test]
    fn test_difficulty_distribution() {
        let config = ToolDatasetConfig::comprehensive();
        let dataset = ToolCallDataset::generate(config).unwrap();

        // Should have examples of all difficulties
        assert!(dataset.stats.by_difficulty.contains_key("Easy"));
        assert!(dataset.stats.by_difficulty.contains_key("Medium"));
        assert!(dataset.stats.by_difficulty.contains_key("Hard"));
        assert!(dataset.stats.by_difficulty.contains_key("Expert"));
    }

    #[test]
    fn test_dataset_split() {
        let config = ToolDatasetConfig::minimal();
        let dataset = ToolCallDataset::generate(config).unwrap();

        let (train, val, test) = dataset.split(0.7, 0.15, 42);

        assert_eq!(train.len() + val.len() + test.len(), dataset.len());
        assert!(train.len() >= val.len());
        assert!(train.len() >= test.len());
    }

    #[test]
    fn test_filter_by_category() {
        let config = ToolDatasetConfig::minimal();
        let dataset = ToolCallDataset::generate(config).unwrap();

        let memory_examples = dataset.filter_by_category(ToolCategory::MemoryOperations);
        for example in memory_examples {
            assert_eq!(example.category, ToolCategory::MemoryOperations);
        }
    }

    #[test]
    fn test_tool_definitions() {
        let tools = ToolDatasetGenerator::define_mcp_tools();

        // Check we have the core tools
        let tool_names: Vec<&str> = tools.iter().map(|t| t.name.as_str()).collect();

        assert!(tool_names.contains(&"agent_spawn"));
        assert!(tool_names.contains(&"memory_store"));
        assert!(tool_names.contains(&"memory_search"));
        assert!(tool_names.contains(&"swarm_init"));
        assert!(tool_names.contains(&"task_create"));
        assert!(tool_names.contains(&"hooks_pre-task"));
    }

    #[test]
    fn test_param_generation() {
        let config = ToolDatasetConfig::minimal();
        let dataset = ToolCallDataset::generate(config).unwrap();

        for example in &dataset.examples {
            // All examples should have params
            assert!(example.expected_params.is_object());
        }
    }

    #[test]
    fn test_quality_scores() {
        let config = ToolDatasetConfig::minimal();
        let dataset = ToolCallDataset::generate(config).unwrap();

        for example in &dataset.examples {
            assert!(example.quality_score >= 0.0);
            assert!(example.quality_score <= 1.0);
        }

        // Average quality should be reasonable
        assert!(dataset.stats.avg_quality > 0.5);
    }
}