cognisagent 0.2.1

Batteries-included agent framework built on cognis and cognisgraph
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
//! Built-in tooling system for DeepAgents.
//!
//! Provides tool specifications, a registry for managing tools with handlers,
//! tool chaining for sequential execution, and usage statistics tracking.
//! This mirrors the Python `cognisagent` `FilesystemMiddleware` tools
//! (read, write, edit, ls, glob, grep) along with additional utility tools.
//!
//! ## Core types
//!
//! - [`ToolSpec`] -- metadata describing a tool (name, description, parameters, category)
//! - [`ToolParam`] -- a typed parameter accepted by a tool
//! - [`ToolCategory`] -- categorisation of tools (Filesystem, Network, Memory, etc.)
//! - [`ToolResult`] -- the result of a tool invocation
//! - [`BuiltinTools`] -- factory for built-in tool specs and handlers
//! - [`ToolRegistry`] -- manages tool specs and handlers with execution support
//! - [`ToolChain`] -- executes a sequence of tools, piping output between steps
//! - [`ToolUsageStats`] -- tracks tool invocation statistics

use std::collections::HashMap;
use std::fmt;
use std::time::Instant;

use serde_json::{json, Value};

// ---------------------------------------------------------------------------
// ToolCategory
// ---------------------------------------------------------------------------

/// Categorisation of a tool.
#[derive(Debug, Clone, PartialEq)]
pub enum ToolCategory {
    /// File system operations (read, write, list, etc.).
    Filesystem,
    /// Network operations (HTTP, etc.).
    Network,
    /// Memory / state management.
    Memory,
    /// Computation and math.
    Computation,
    /// Search and query operations.
    Search,
    /// A user-defined category.
    Custom(String),
}

impl ToolCategory {
    /// Returns a string representation of the category.
    pub fn as_str(&self) -> &str {
        match self {
            Self::Filesystem => "filesystem",
            Self::Network => "network",
            Self::Memory => "memory",
            Self::Computation => "computation",
            Self::Search => "search",
            Self::Custom(s) => s.as_str(),
        }
    }
}

impl fmt::Display for ToolCategory {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

// ---------------------------------------------------------------------------
// ToolParam
// ---------------------------------------------------------------------------

/// Describes a parameter accepted by a [`ToolSpec`].
#[derive(Debug, Clone)]
pub struct ToolParam {
    /// Parameter name.
    pub name: String,
    /// Expected type as a string (e.g. "string", "number", "boolean").
    pub param_type: String,
    /// Whether this parameter must be provided.
    pub required: bool,
    /// Human-readable description.
    pub description: String,
    /// Default value used when the parameter is not supplied.
    pub default_value: Option<Value>,
}

impl ToolParam {
    /// Create a new required parameter.
    pub fn new(name: impl Into<String>, param_type: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            param_type: param_type.into(),
            required: true,
            description: String::new(),
            default_value: None,
        }
    }

    /// Mark as optional.
    pub fn optional(mut self) -> Self {
        self.required = false;
        self
    }

    /// Set required flag.
    pub fn with_required(mut self, required: bool) -> Self {
        self.required = required;
        self
    }

    /// Set the description.
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = description.into();
        self
    }

    /// Set a default value.
    pub fn with_default(mut self, value: Value) -> Self {
        self.default_value = Some(value);
        self
    }
}

// ---------------------------------------------------------------------------
// ToolSpec
// ---------------------------------------------------------------------------

/// Metadata describing a tool.
#[derive(Debug, Clone)]
pub struct ToolSpec {
    /// Unique tool name.
    pub name: String,
    /// Human-readable description.
    pub description: String,
    /// Parameters accepted by this tool.
    pub parameters: Vec<ToolParam>,
    /// Description of what the tool returns.
    pub returns: String,
    /// Tool category.
    pub category: ToolCategory,
}

impl ToolSpec {
    /// Create a new spec with the given name.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            description: String::new(),
            parameters: Vec::new(),
            returns: String::new(),
            category: ToolCategory::Custom("uncategorized".into()),
        }
    }

    /// Set the description.
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = description.into();
        self
    }

    /// Add a parameter.
    pub fn with_param(mut self, param: ToolParam) -> Self {
        self.parameters.push(param);
        self
    }

    /// Set the return description.
    pub fn with_returns(mut self, returns: impl Into<String>) -> Self {
        self.returns = returns.into();
        self
    }

    /// Set the category.
    pub fn with_category(mut self, category: ToolCategory) -> Self {
        self.category = category;
        self
    }

    /// Serialize this spec to a JSON value.
    pub fn to_json(&self) -> Value {
        let params: Vec<Value> = self
            .parameters
            .iter()
            .map(|p| {
                json!({
                    "name": p.name,
                    "type": p.param_type,
                    "required": p.required,
                    "description": p.description,
                    "default": p.default_value,
                })
            })
            .collect();
        json!({
            "name": self.name,
            "description": self.description,
            "parameters": params,
            "returns": self.returns,
            "category": self.category.as_str(),
        })
    }

    /// Validate the provided arguments against parameter definitions.
    ///
    /// Checks that all required parameters are present in the JSON object.
    pub fn validate_args(&self, args: &Value) -> Result<(), String> {
        let obj = args
            .as_object()
            .ok_or_else(|| "args must be a JSON object".to_string())?;
        for param in &self.parameters {
            if param.required && !obj.contains_key(&param.name) && param.default_value.is_none() {
                return Err(format!("missing required parameter '{}'", param.name));
            }
        }
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// ToolResult
// ---------------------------------------------------------------------------

/// The result of a tool invocation.
#[derive(Debug, Clone)]
pub struct ToolResult {
    /// Whether the tool executed successfully.
    pub success: bool,
    /// The output value.
    pub output: Value,
    /// Error message if the execution failed.
    pub error: Option<String>,
    /// Execution duration in milliseconds.
    pub duration_ms: u64,
    /// Name of the tool that produced this result.
    pub tool_name: String,
}

impl ToolResult {
    /// Serialize to a JSON value.
    pub fn to_json(&self) -> Value {
        json!({
            "success": self.success,
            "output": self.output,
            "error": self.error,
            "duration_ms": self.duration_ms,
            "tool_name": self.tool_name,
        })
    }

    /// Returns `true` if the result represents an error.
    pub fn is_error(&self) -> bool {
        !self.success
    }
}

// ---------------------------------------------------------------------------
// BuiltinTools
// ---------------------------------------------------------------------------

/// Type alias for a tool handler function.
pub type ToolHandler = Box<dyn Fn(Value) -> ToolResult + Send + Sync>;

/// Provides built-in tool implementations.
pub struct BuiltinTools;

impl BuiltinTools {
    /// Returns the spec and handler for a simulated file-read tool.
    pub fn read_file() -> (ToolSpec, ToolHandler) {
        let spec = ToolSpec::new("read_file")
            .with_description("Read the contents of a file at the given path")
            .with_param(
                ToolParam::new("path", "string").with_description("Path to the file to read"),
            )
            .with_returns("File contents as a string")
            .with_category(ToolCategory::Filesystem);

        let handler: ToolHandler = Box::new(|args: Value| {
            let start = Instant::now();
            let path = args.get("path").and_then(|v| v.as_str()).unwrap_or("");
            if path.is_empty() {
                return ToolResult {
                    success: false,
                    output: Value::Null,
                    error: Some("path is required".into()),
                    duration_ms: start.elapsed().as_millis() as u64,
                    tool_name: "read_file".into(),
                };
            }
            // Simulated file read
            ToolResult {
                success: true,
                output: json!(format!("Contents of file: {}", path)),
                error: None,
                duration_ms: start.elapsed().as_millis() as u64,
                tool_name: "read_file".into(),
            }
        });

        (spec, handler)
    }

    /// Returns the spec and handler for a simulated file-write tool.
    pub fn write_file() -> (ToolSpec, ToolHandler) {
        let spec = ToolSpec::new("write_file")
            .with_description("Write content to a file at the given path")
            .with_param(
                ToolParam::new("path", "string").with_description("Path to the file to write"),
            )
            .with_param(
                ToolParam::new("content", "string")
                    .with_description("Content to write to the file"),
            )
            .with_returns("Confirmation message")
            .with_category(ToolCategory::Filesystem);

        let handler: ToolHandler = Box::new(|args: Value| {
            let start = Instant::now();
            let path = args.get("path").and_then(|v| v.as_str()).unwrap_or("");
            let content = args.get("content").and_then(|v| v.as_str()).unwrap_or("");
            if path.is_empty() {
                return ToolResult {
                    success: false,
                    output: Value::Null,
                    error: Some("path is required".into()),
                    duration_ms: start.elapsed().as_millis() as u64,
                    tool_name: "write_file".into(),
                };
            }
            // Simulated file write
            ToolResult {
                success: true,
                output: json!(format!("Wrote {} bytes to {}", content.len(), path)),
                error: None,
                duration_ms: start.elapsed().as_millis() as u64,
                tool_name: "write_file".into(),
            }
        });

        (spec, handler)
    }

    /// Returns the spec and handler for a simulated directory-listing tool.
    pub fn list_directory() -> (ToolSpec, ToolHandler) {
        let spec = ToolSpec::new("list_directory")
            .with_description("List the contents of a directory")
            .with_param(
                ToolParam::new("path", "string")
                    .with_description("Path to the directory")
                    .optional()
                    .with_default(json!(".")),
            )
            .with_returns("Array of file/directory names")
            .with_category(ToolCategory::Filesystem);

        let handler: ToolHandler = Box::new(|args: Value| {
            let start = Instant::now();
            let path = args.get("path").and_then(|v| v.as_str()).unwrap_or(".");
            // Simulated directory listing
            ToolResult {
                success: true,
                output: json!({
                    "path": path,
                    "entries": ["file1.txt", "file2.rs", "src/", "Cargo.toml"]
                }),
                error: None,
                duration_ms: start.elapsed().as_millis() as u64,
                tool_name: "list_directory".into(),
            }
        });

        (spec, handler)
    }

    /// Returns the spec and handler for a text search tool (actual implementation).
    pub fn search_text() -> (ToolSpec, ToolHandler) {
        let spec = ToolSpec::new("search_text")
            .with_description("Search for a pattern in the given text content")
            .with_param(
                ToolParam::new("content", "string")
                    .with_description("The text content to search in"),
            )
            .with_param(
                ToolParam::new("pattern", "string").with_description("The pattern to search for"),
            )
            .with_returns("Array of matching lines with line numbers")
            .with_category(ToolCategory::Search);

        let handler: ToolHandler = Box::new(|args: Value| {
            let start = Instant::now();
            let content = args.get("content").and_then(|v| v.as_str()).unwrap_or("");
            let pattern = args.get("pattern").and_then(|v| v.as_str()).unwrap_or("");

            if pattern.is_empty() {
                return ToolResult {
                    success: false,
                    output: Value::Null,
                    error: Some("pattern is required".into()),
                    duration_ms: start.elapsed().as_millis() as u64,
                    tool_name: "search_text".into(),
                };
            }

            let matches: Vec<Value> = content
                .lines()
                .enumerate()
                .filter(|(_, line)| line.contains(pattern))
                .map(|(i, line)| {
                    json!({
                        "line_number": i + 1,
                        "content": line,
                    })
                })
                .collect();

            ToolResult {
                success: true,
                output: json!({
                    "pattern": pattern,
                    "match_count": matches.len(),
                    "matches": matches,
                }),
                error: None,
                duration_ms: start.elapsed().as_millis() as u64,
                tool_name: "search_text".into(),
            }
        });

        (spec, handler)
    }

    /// Returns the spec and handler for a simple calculator tool.
    pub fn calculate() -> (ToolSpec, ToolHandler) {
        let spec = ToolSpec::new("calculate")
            .with_description("Evaluate a simple math expression (+, -, *, /)")
            .with_param(ToolParam::new("left", "number").with_description("Left operand"))
            .with_param(
                ToolParam::new("operator", "string").with_description("Operator: +, -, *, /"),
            )
            .with_param(ToolParam::new("right", "number").with_description("Right operand"))
            .with_returns("The numeric result")
            .with_category(ToolCategory::Computation);

        let handler: ToolHandler = Box::new(|args: Value| {
            let start = Instant::now();
            let left = args.get("left").and_then(|v| v.as_f64());
            let right = args.get("right").and_then(|v| v.as_f64());
            let operator = args.get("operator").and_then(|v| v.as_str()).unwrap_or("");

            let (left, right) = match (left, right) {
                (Some(l), Some(r)) => (l, r),
                _ => {
                    return ToolResult {
                        success: false,
                        output: Value::Null,
                        error: Some("left and right must be numbers".into()),
                        duration_ms: start.elapsed().as_millis() as u64,
                        tool_name: "calculate".into(),
                    };
                }
            };

            let result = match operator {
                "+" => Ok(left + right),
                "-" => Ok(left - right),
                "*" => Ok(left * right),
                "/" => {
                    if right == 0.0 {
                        Err("division by zero".to_string())
                    } else {
                        Ok(left / right)
                    }
                }
                _ => Err(format!("unsupported operator '{}'", operator)),
            };

            match result {
                Ok(val) => ToolResult {
                    success: true,
                    output: json!(val),
                    error: None,
                    duration_ms: start.elapsed().as_millis() as u64,
                    tool_name: "calculate".into(),
                },
                Err(e) => ToolResult {
                    success: false,
                    output: Value::Null,
                    error: Some(e),
                    duration_ms: start.elapsed().as_millis() as u64,
                    tool_name: "calculate".into(),
                },
            }
        });

        (spec, handler)
    }

    /// Returns the spec and handler for a JSON query tool using dot-notation paths.
    pub fn json_query() -> (ToolSpec, ToolHandler) {
        let spec = ToolSpec::new("json_query")
            .with_description("Extract data from a JSON value using dot-notation paths")
            .with_param(ToolParam::new("data", "json").with_description("The JSON data to query"))
            .with_param(
                ToolParam::new("path", "string")
                    .with_description("Dot-notation path (e.g. 'user.name')"),
            )
            .with_returns("The extracted value")
            .with_category(ToolCategory::Search);

        let handler: ToolHandler = Box::new(|args: Value| {
            let start = Instant::now();
            let data = args.get("data").cloned().unwrap_or(Value::Null);
            let path = args.get("path").and_then(|v| v.as_str()).unwrap_or("");

            if path.is_empty() {
                return ToolResult {
                    success: false,
                    output: Value::Null,
                    error: Some("path is required".into()),
                    duration_ms: start.elapsed().as_millis() as u64,
                    tool_name: "json_query".into(),
                };
            }

            let mut current = &data;
            for segment in path.split('.') {
                // Try as object key first
                if let Some(val) = current.get(segment) {
                    current = val;
                } else if let Ok(idx) = segment.parse::<usize>() {
                    // Try as array index
                    if let Some(val) = current.get(idx) {
                        current = val;
                    } else {
                        return ToolResult {
                            success: false,
                            output: Value::Null,
                            error: Some(format!("path segment '{}' not found", segment)),
                            duration_ms: start.elapsed().as_millis() as u64,
                            tool_name: "json_query".into(),
                        };
                    }
                } else {
                    return ToolResult {
                        success: false,
                        output: Value::Null,
                        error: Some(format!("path segment '{}' not found", segment)),
                        duration_ms: start.elapsed().as_millis() as u64,
                        tool_name: "json_query".into(),
                    };
                }
            }

            ToolResult {
                success: true,
                output: current.clone(),
                error: None,
                duration_ms: start.elapsed().as_millis() as u64,
                tool_name: "json_query".into(),
            }
        });

        (spec, handler)
    }
}

// ---------------------------------------------------------------------------
// ToolRegistry
// ---------------------------------------------------------------------------

/// Manages tool specs and handlers with execution support.
pub struct ToolRegistry {
    specs: HashMap<String, ToolSpec>,
    handlers: HashMap<String, ToolHandler>,
}

impl ToolRegistry {
    /// Create an empty registry.
    pub fn new() -> Self {
        Self {
            specs: HashMap::new(),
            handlers: HashMap::new(),
        }
    }

    /// Register a tool spec with its handler.
    pub fn register(
        &mut self,
        spec: ToolSpec,
        handler: Box<dyn Fn(Value) -> ToolResult + Send + Sync>,
    ) {
        let name = spec.name.clone();
        self.specs.insert(name.clone(), spec);
        self.handlers.insert(name, handler);
    }

    /// Execute a tool by name with the given arguments.
    pub fn execute(&self, name: &str, args: Value) -> Result<ToolResult, String> {
        let spec = self
            .specs
            .get(name)
            .ok_or_else(|| format!("tool '{}' not found", name))?;
        spec.validate_args(&args)?;
        let handler = self
            .handlers
            .get(name)
            .ok_or_else(|| format!("handler for '{}' not found", name))?;
        Ok(handler(args))
    }

    /// Get a reference to a tool spec by name.
    pub fn get_spec(&self, name: &str) -> Option<&ToolSpec> {
        self.specs.get(name)
    }

    /// Return all tool specs matching a given category.
    pub fn by_category(&self, category: &ToolCategory) -> Vec<&ToolSpec> {
        self.specs
            .values()
            .filter(|s| &s.category == category)
            .collect()
    }

    /// Return references to all registered tool specs.
    pub fn all_specs(&self) -> Vec<&ToolSpec> {
        self.specs.values().collect()
    }

    /// Return the number of registered tools.
    pub fn len(&self) -> usize {
        self.specs.len()
    }

    /// Returns `true` if the registry has no tools.
    pub fn is_empty(&self) -> bool {
        self.specs.is_empty()
    }

    /// Serialize the registry to a JSON array of tool specs.
    pub fn to_json(&self) -> Value {
        let arr: Vec<Value> = self.specs.values().map(|s| s.to_json()).collect();
        Value::Array(arr)
    }
}

impl Default for ToolRegistry {
    fn default() -> Self {
        Self::new()
    }
}

// ---------------------------------------------------------------------------
// ToolChain
// ---------------------------------------------------------------------------

/// A step in a tool chain.
#[derive(Debug, Clone)]
struct ToolChainStep {
    tool_name: String,
    args_template: Value,
}

/// Executes a sequence of tool calls, piping output from one step to the next.
pub struct ToolChain {
    steps: Vec<ToolChainStep>,
}

impl ToolChain {
    /// Create an empty tool chain.
    pub fn new() -> Self {
        Self { steps: Vec::new() }
    }

    /// Add a step to the chain.
    pub fn add_step(&mut self, tool_name: String, args_template: Value) {
        self.steps.push(ToolChainStep {
            tool_name,
            args_template,
        });
    }

    /// Execute all steps in sequence, merging the previous step's output into
    /// the next step's args under the key `"_previous"`.
    pub fn execute(
        &self,
        registry: &ToolRegistry,
        initial_input: Value,
    ) -> Result<Vec<ToolResult>, String> {
        let mut results = Vec::new();
        let mut previous_output = initial_input;

        for step in &self.steps {
            let mut args = step.args_template.clone();
            if let Some(obj) = args.as_object_mut() {
                obj.insert("_previous".into(), previous_output.clone());
            }

            let result = registry.execute(&step.tool_name, args)?;
            if result.is_error() {
                results.push(result);
                return Ok(results);
            }
            previous_output = result.output.clone();
            results.push(result);
        }

        Ok(results)
    }

    /// Return the number of steps in the chain.
    pub fn step_count(&self) -> usize {
        self.steps.len()
    }
}

impl Default for ToolChain {
    fn default() -> Self {
        Self::new()
    }
}

// ---------------------------------------------------------------------------
// ToolUsageStats
// ---------------------------------------------------------------------------

/// Summary statistics for a single tool.
#[derive(Debug, Clone)]
pub struct ToolUsageSummary {
    /// Total number of calls.
    pub call_count: u64,
    /// Number of successful calls.
    pub success_count: u64,
    /// Number of failed calls.
    pub failure_count: u64,
    /// Total duration in milliseconds across all calls.
    pub total_duration_ms: u64,
    /// Average duration per call in milliseconds.
    pub avg_duration_ms: f64,
}

/// Tracks tool invocation statistics.
pub struct ToolUsageStats {
    stats: HashMap<String, ToolUsageSummary>,
}

impl ToolUsageStats {
    /// Create an empty stats tracker.
    pub fn new() -> Self {
        Self {
            stats: HashMap::new(),
        }
    }

    /// Record a tool invocation.
    pub fn record(&mut self, tool_name: &str, duration_ms: u64, success: bool) {
        let entry = self
            .stats
            .entry(tool_name.to_string())
            .or_insert_with(|| ToolUsageSummary {
                call_count: 0,
                success_count: 0,
                failure_count: 0,
                total_duration_ms: 0,
                avg_duration_ms: 0.0,
            });
        entry.call_count += 1;
        if success {
            entry.success_count += 1;
        } else {
            entry.failure_count += 1;
        }
        entry.total_duration_ms += duration_ms;
        entry.avg_duration_ms = entry.total_duration_ms as f64 / entry.call_count as f64;
    }

    /// Get usage summary for a specific tool.
    pub fn by_tool(&self, name: &str) -> Option<&ToolUsageSummary> {
        self.stats.get(name)
    }

    /// Return the name of the most-used tool, or `None` if no calls have been recorded.
    pub fn most_used(&self) -> Option<String> {
        self.stats
            .iter()
            .max_by_key(|(_, s)| s.call_count)
            .map(|(name, _)| name.clone())
    }

    /// Return the total number of calls across all tools.
    pub fn total_calls(&self) -> u64 {
        self.stats.values().map(|s| s.call_count).sum()
    }

    /// Serialize to a JSON value.
    pub fn to_json(&self) -> Value {
        let map: serde_json::Map<String, Value> = self
            .stats
            .iter()
            .map(|(name, s)| {
                (
                    name.clone(),
                    json!({
                        "call_count": s.call_count,
                        "success_count": s.success_count,
                        "failure_count": s.failure_count,
                        "total_duration_ms": s.total_duration_ms,
                        "avg_duration_ms": s.avg_duration_ms,
                    }),
                )
            })
            .collect();
        Value::Object(map)
    }
}

impl Default for ToolUsageStats {
    fn default() -> Self {
        Self::new()
    }
}

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

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

    // -- ToolCategory --

    #[test]
    fn test_category_filesystem_as_str() {
        assert_eq!(ToolCategory::Filesystem.as_str(), "filesystem");
    }

    #[test]
    fn test_category_network_as_str() {
        assert_eq!(ToolCategory::Network.as_str(), "network");
    }

    #[test]
    fn test_category_memory_as_str() {
        assert_eq!(ToolCategory::Memory.as_str(), "memory");
    }

    #[test]
    fn test_category_computation_as_str() {
        assert_eq!(ToolCategory::Computation.as_str(), "computation");
    }

    #[test]
    fn test_category_search_as_str() {
        assert_eq!(ToolCategory::Search.as_str(), "search");
    }

    #[test]
    fn test_category_custom_as_str() {
        assert_eq!(ToolCategory::Custom("my_cat".into()).as_str(), "my_cat");
    }

    #[test]
    fn test_category_display() {
        assert_eq!(format!("{}", ToolCategory::Filesystem), "filesystem");
        assert_eq!(format!("{}", ToolCategory::Custom("agent".into())), "agent");
    }

    // -- ToolParam --

    #[test]
    fn test_param_new_defaults() {
        let p = ToolParam::new("path", "string");
        assert_eq!(p.name, "path");
        assert_eq!(p.param_type, "string");
        assert!(p.required);
        assert_eq!(p.description, "");
        assert!(p.default_value.is_none());
    }

    #[test]
    fn test_param_builder_chain() {
        let p = ToolParam::new("count", "number")
            .optional()
            .with_description("Number of items")
            .with_default(json!(10));
        assert!(!p.required);
        assert_eq!(p.description, "Number of items");
        assert_eq!(p.default_value, Some(json!(10)));
    }

    #[test]
    fn test_param_with_required() {
        let p = ToolParam::new("x", "string").with_required(false);
        assert!(!p.required);
        let p2 = p.with_required(true);
        assert!(p2.required);
    }

    // -- ToolSpec --

    #[test]
    fn test_spec_new_defaults() {
        let s = ToolSpec::new("test_tool");
        assert_eq!(s.name, "test_tool");
        assert_eq!(s.description, "");
        assert!(s.parameters.is_empty());
        assert_eq!(s.returns, "");
    }

    #[test]
    fn test_spec_builder_chain() {
        let s = ToolSpec::new("read")
            .with_description("Read a file")
            .with_param(ToolParam::new("path", "string"))
            .with_returns("File contents")
            .with_category(ToolCategory::Filesystem);
        assert_eq!(s.name, "read");
        assert_eq!(s.description, "Read a file");
        assert_eq!(s.parameters.len(), 1);
        assert_eq!(s.returns, "File contents");
        assert_eq!(s.category, ToolCategory::Filesystem);
    }

    #[test]
    fn test_spec_to_json() {
        let s = ToolSpec::new("calc")
            .with_description("Calculate")
            .with_param(ToolParam::new("x", "number").with_description("operand"))
            .with_returns("result")
            .with_category(ToolCategory::Computation);
        let j = s.to_json();
        assert_eq!(j["name"], "calc");
        assert_eq!(j["description"], "Calculate");
        assert_eq!(j["category"], "computation");
        assert_eq!(j["returns"], "result");
        assert_eq!(j["parameters"].as_array().unwrap().len(), 1);
        assert_eq!(j["parameters"][0]["name"], "x");
    }

    #[test]
    fn test_spec_validate_args_ok() {
        let s = ToolSpec::new("t")
            .with_param(ToolParam::new("a", "string"))
            .with_param(ToolParam::new("b", "number").optional());
        let args = json!({"a": "hello"});
        assert!(s.validate_args(&args).is_ok());
    }

    #[test]
    fn test_spec_validate_args_missing_required() {
        let s = ToolSpec::new("t").with_param(ToolParam::new("a", "string"));
        let args = json!({});
        let err = s.validate_args(&args);
        assert!(err.is_err());
        assert!(err.unwrap_err().contains("a"));
    }

    #[test]
    fn test_spec_validate_args_not_object() {
        let s = ToolSpec::new("t");
        let err = s.validate_args(&json!("not an object"));
        assert!(err.is_err());
    }

    #[test]
    fn test_spec_validate_args_with_default_ok() {
        let s = ToolSpec::new("t")
            .with_param(ToolParam::new("a", "string").with_default(json!("default_val")));
        let args = json!({});
        assert!(s.validate_args(&args).is_ok());
    }

    // -- ToolResult --

    #[test]
    fn test_result_success() {
        let r = ToolResult {
            success: true,
            output: json!("ok"),
            error: None,
            duration_ms: 5,
            tool_name: "test".into(),
        };
        assert!(!r.is_error());
        let j = r.to_json();
        assert_eq!(j["success"], true);
        assert_eq!(j["output"], "ok");
        assert!(j["error"].is_null());
    }

    #[test]
    fn test_result_error() {
        let r = ToolResult {
            success: false,
            output: Value::Null,
            error: Some("boom".into()),
            duration_ms: 1,
            tool_name: "test".into(),
        };
        assert!(r.is_error());
        let j = r.to_json();
        assert_eq!(j["error"], "boom");
    }

    // -- BuiltinTools: read_file --

    #[test]
    fn test_builtin_read_file_success() {
        let (spec, handler) = BuiltinTools::read_file();
        assert_eq!(spec.name, "read_file");
        assert_eq!(spec.category, ToolCategory::Filesystem);
        let result = handler(json!({"path": "/tmp/test.txt"}));
        assert!(result.success);
        assert!(result.output.as_str().unwrap().contains("/tmp/test.txt"));
    }

    #[test]
    fn test_builtin_read_file_empty_path() {
        let (_, handler) = BuiltinTools::read_file();
        let result = handler(json!({"path": ""}));
        assert!(result.is_error());
    }

    #[test]
    fn test_builtin_read_file_missing_path() {
        let (_, handler) = BuiltinTools::read_file();
        let result = handler(json!({}));
        assert!(result.is_error());
    }

    // -- BuiltinTools: write_file --

    #[test]
    fn test_builtin_write_file_success() {
        let (spec, handler) = BuiltinTools::write_file();
        assert_eq!(spec.name, "write_file");
        let result = handler(json!({"path": "/tmp/out.txt", "content": "hello"}));
        assert!(result.success);
        assert!(result.output.as_str().unwrap().contains("5 bytes"));
    }

    #[test]
    fn test_builtin_write_file_empty_path() {
        let (_, handler) = BuiltinTools::write_file();
        let result = handler(json!({"path": "", "content": "data"}));
        assert!(result.is_error());
    }

    // -- BuiltinTools: list_directory --

    #[test]
    fn test_builtin_list_directory_success() {
        let (spec, handler) = BuiltinTools::list_directory();
        assert_eq!(spec.name, "list_directory");
        let result = handler(json!({"path": "/tmp"}));
        assert!(result.success);
        assert!(result.output.get("entries").is_some());
    }

    #[test]
    fn test_builtin_list_directory_default_path() {
        let (_, handler) = BuiltinTools::list_directory();
        let result = handler(json!({}));
        assert!(result.success);
        assert_eq!(result.output["path"], ".");
    }

    // -- BuiltinTools: search_text --

    #[test]
    fn test_builtin_search_text_found() {
        let (spec, handler) = BuiltinTools::search_text();
        assert_eq!(spec.name, "search_text");
        assert_eq!(spec.category, ToolCategory::Search);
        let result = handler(json!({
            "content": "hello world\nfoo bar\nhello again",
            "pattern": "hello"
        }));
        assert!(result.success);
        assert_eq!(result.output["match_count"], 2);
        let matches = result.output["matches"].as_array().unwrap();
        assert_eq!(matches[0]["line_number"], 1);
        assert_eq!(matches[1]["line_number"], 3);
    }

    #[test]
    fn test_builtin_search_text_not_found() {
        let (_, handler) = BuiltinTools::search_text();
        let result = handler(json!({
            "content": "hello world",
            "pattern": "xyz"
        }));
        assert!(result.success);
        assert_eq!(result.output["match_count"], 0);
    }

    #[test]
    fn test_builtin_search_text_empty_pattern() {
        let (_, handler) = BuiltinTools::search_text();
        let result = handler(json!({"content": "hello", "pattern": ""}));
        assert!(result.is_error());
    }

    // -- BuiltinTools: calculate --

    #[test]
    fn test_builtin_calculate_add() {
        let (spec, handler) = BuiltinTools::calculate();
        assert_eq!(spec.name, "calculate");
        assert_eq!(spec.category, ToolCategory::Computation);
        let result = handler(json!({"left": 2, "operator": "+", "right": 3}));
        assert!(result.success);
        assert_eq!(result.output, json!(5.0));
    }

    #[test]
    fn test_builtin_calculate_subtract() {
        let (_, handler) = BuiltinTools::calculate();
        let result = handler(json!({"left": 10, "operator": "-", "right": 4}));
        assert!(result.success);
        assert_eq!(result.output, json!(6.0));
    }

    #[test]
    fn test_builtin_calculate_multiply() {
        let (_, handler) = BuiltinTools::calculate();
        let result = handler(json!({"left": 3, "operator": "*", "right": 7}));
        assert!(result.success);
        assert_eq!(result.output, json!(21.0));
    }

    #[test]
    fn test_builtin_calculate_divide() {
        let (_, handler) = BuiltinTools::calculate();
        let result = handler(json!({"left": 15, "operator": "/", "right": 3}));
        assert!(result.success);
        assert_eq!(result.output, json!(5.0));
    }

    #[test]
    fn test_builtin_calculate_division_by_zero() {
        let (_, handler) = BuiltinTools::calculate();
        let result = handler(json!({"left": 5, "operator": "/", "right": 0}));
        assert!(result.is_error());
        assert!(result.error.unwrap().contains("division by zero"));
    }

    #[test]
    fn test_builtin_calculate_unsupported_operator() {
        let (_, handler) = BuiltinTools::calculate();
        let result = handler(json!({"left": 1, "operator": "%", "right": 2}));
        assert!(result.is_error());
        assert!(result.error.unwrap().contains("unsupported operator"));
    }

    #[test]
    fn test_builtin_calculate_invalid_operands() {
        let (_, handler) = BuiltinTools::calculate();
        let result = handler(json!({"left": "not_a_number", "operator": "+", "right": 1}));
        assert!(result.is_error());
    }

    // -- BuiltinTools: json_query --

    #[test]
    fn test_builtin_json_query_simple() {
        let (spec, handler) = BuiltinTools::json_query();
        assert_eq!(spec.name, "json_query");
        let result = handler(json!({
            "data": {"name": "Alice", "age": 30},
            "path": "name"
        }));
        assert!(result.success);
        assert_eq!(result.output, json!("Alice"));
    }

    #[test]
    fn test_builtin_json_query_nested() {
        let (_, handler) = BuiltinTools::json_query();
        let result = handler(json!({
            "data": {"user": {"profile": {"name": "Bob"}}},
            "path": "user.profile.name"
        }));
        assert!(result.success);
        assert_eq!(result.output, json!("Bob"));
    }

    #[test]
    fn test_builtin_json_query_array_index() {
        let (_, handler) = BuiltinTools::json_query();
        let result = handler(json!({
            "data": {"items": ["a", "b", "c"]},
            "path": "items.1"
        }));
        assert!(result.success);
        assert_eq!(result.output, json!("b"));
    }

    #[test]
    fn test_builtin_json_query_missing_path() {
        let (_, handler) = BuiltinTools::json_query();
        let result = handler(json!({"data": {"a": 1}, "path": "b"}));
        assert!(result.is_error());
        assert!(result.error.unwrap().contains("not found"));
    }

    #[test]
    fn test_builtin_json_query_empty_path() {
        let (_, handler) = BuiltinTools::json_query();
        let result = handler(json!({"data": {"a": 1}, "path": ""}));
        assert!(result.is_error());
    }

    // -- ToolRegistry --

    #[test]
    fn test_registry_new_empty() {
        let reg = ToolRegistry::new();
        assert!(reg.is_empty());
        assert_eq!(reg.len(), 0);
    }

    #[test]
    fn test_registry_default() {
        let reg = ToolRegistry::default();
        assert!(reg.is_empty());
    }

    #[test]
    fn test_registry_register_and_get_spec() {
        let mut reg = ToolRegistry::new();
        let (spec, handler) = BuiltinTools::calculate();
        reg.register(spec, handler);
        assert_eq!(reg.len(), 1);
        let s = reg.get_spec("calculate").unwrap();
        assert_eq!(s.name, "calculate");
    }

    #[test]
    fn test_registry_get_spec_missing() {
        let reg = ToolRegistry::new();
        assert!(reg.get_spec("nope").is_none());
    }

    #[test]
    fn test_registry_execute_success() {
        let mut reg = ToolRegistry::new();
        let (spec, handler) = BuiltinTools::calculate();
        reg.register(spec, handler);
        let result = reg
            .execute("calculate", json!({"left": 1, "operator": "+", "right": 2}))
            .unwrap();
        assert!(result.success);
        assert_eq!(result.output, json!(3.0));
    }

    #[test]
    fn test_registry_execute_missing_tool() {
        let reg = ToolRegistry::new();
        let err = reg.execute("missing", json!({}));
        assert!(err.is_err());
        assert!(err.unwrap_err().contains("not found"));
    }

    #[test]
    fn test_registry_execute_invalid_args() {
        let mut reg = ToolRegistry::new();
        let (spec, handler) = BuiltinTools::calculate();
        reg.register(spec, handler);
        let err = reg.execute("calculate", json!({"operator": "+", "right": 2}));
        assert!(err.is_err());
        assert!(err.unwrap_err().contains("left"));
    }

    #[test]
    fn test_registry_by_category() {
        let mut reg = ToolRegistry::new();
        let (s1, h1) = BuiltinTools::read_file();
        let (s2, h2) = BuiltinTools::write_file();
        let (s3, h3) = BuiltinTools::calculate();
        reg.register(s1, h1);
        reg.register(s2, h2);
        reg.register(s3, h3);
        let fs_tools = reg.by_category(&ToolCategory::Filesystem);
        assert_eq!(fs_tools.len(), 2);
        let comp_tools = reg.by_category(&ToolCategory::Computation);
        assert_eq!(comp_tools.len(), 1);
    }

    #[test]
    fn test_registry_all_specs() {
        let mut reg = ToolRegistry::new();
        let (s1, h1) = BuiltinTools::read_file();
        let (s2, h2) = BuiltinTools::calculate();
        reg.register(s1, h1);
        reg.register(s2, h2);
        assert_eq!(reg.all_specs().len(), 2);
    }

    #[test]
    fn test_registry_to_json() {
        let mut reg = ToolRegistry::new();
        let (s, h) = BuiltinTools::search_text();
        reg.register(s, h);
        let j = reg.to_json();
        assert!(j.is_array());
        assert_eq!(j.as_array().unwrap().len(), 1);
        assert_eq!(j[0]["name"], "search_text");
    }

    // -- ToolChain --

    #[test]
    fn test_chain_empty() {
        let chain = ToolChain::new();
        assert_eq!(chain.step_count(), 0);
    }

    #[test]
    fn test_chain_add_step() {
        let mut chain = ToolChain::new();
        chain.add_step(
            "calculate".into(),
            json!({"left": 1, "operator": "+", "right": 2}),
        );
        assert_eq!(chain.step_count(), 1);
    }

    #[test]
    fn test_chain_execute_single_step() {
        let mut reg = ToolRegistry::new();
        let (s, h) = BuiltinTools::calculate();
        reg.register(s, h);

        let mut chain = ToolChain::new();
        chain.add_step(
            "calculate".into(),
            json!({"left": 5, "operator": "*", "right": 3}),
        );
        let results = chain.execute(&reg, json!(null)).unwrap();
        assert_eq!(results.len(), 1);
        assert!(results[0].success);
        assert_eq!(results[0].output, json!(15.0));
    }

    #[test]
    fn test_chain_execute_multiple_steps() {
        let mut reg = ToolRegistry::new();
        let (s1, h1) = BuiltinTools::calculate();
        let (s2, h2) = BuiltinTools::search_text();
        reg.register(s1, h1);
        reg.register(s2, h2);

        let mut chain = ToolChain::new();
        chain.add_step(
            "calculate".into(),
            json!({"left": 2, "operator": "+", "right": 3}),
        );
        chain.add_step(
            "search_text".into(),
            json!({"content": "line one\nline two\nline three", "pattern": "two"}),
        );
        let results = chain.execute(&reg, json!(null)).unwrap();
        assert_eq!(results.len(), 2);
        assert!(results[0].success);
        assert!(results[1].success);
        assert_eq!(results[1].output["match_count"], 1);
    }

    #[test]
    fn test_chain_stops_on_error() {
        let mut reg = ToolRegistry::new();
        let (s, h) = BuiltinTools::calculate();
        reg.register(s, h);

        let mut chain = ToolChain::new();
        chain.add_step(
            "calculate".into(),
            json!({"left": 1, "operator": "/", "right": 0}),
        );
        chain.add_step(
            "calculate".into(),
            json!({"left": 10, "operator": "+", "right": 20}),
        );
        let results = chain.execute(&reg, json!(null)).unwrap();
        // Should stop after the first error
        assert_eq!(results.len(), 1);
        assert!(results[0].is_error());
    }

    #[test]
    fn test_chain_missing_tool_returns_error() {
        let reg = ToolRegistry::new();
        let mut chain = ToolChain::new();
        chain.add_step("nonexistent".into(), json!({}));
        let err = chain.execute(&reg, json!(null));
        assert!(err.is_err());
    }

    // -- ToolUsageStats --

    #[test]
    fn test_stats_new_empty() {
        let stats = ToolUsageStats::new();
        assert_eq!(stats.total_calls(), 0);
        assert!(stats.most_used().is_none());
    }

    #[test]
    fn test_stats_record_and_by_tool() {
        let mut stats = ToolUsageStats::new();
        stats.record("calc", 10, true);
        stats.record("calc", 20, false);
        let summary = stats.by_tool("calc").unwrap();
        assert_eq!(summary.call_count, 2);
        assert_eq!(summary.success_count, 1);
        assert_eq!(summary.failure_count, 1);
        assert_eq!(summary.total_duration_ms, 30);
        assert!((summary.avg_duration_ms - 15.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_stats_by_tool_missing() {
        let stats = ToolUsageStats::new();
        assert!(stats.by_tool("nope").is_none());
    }

    #[test]
    fn test_stats_most_used() {
        let mut stats = ToolUsageStats::new();
        stats.record("a", 1, true);
        stats.record("b", 1, true);
        stats.record("b", 1, true);
        stats.record("c", 1, true);
        assert_eq!(stats.most_used().unwrap(), "b");
    }

    #[test]
    fn test_stats_total_calls() {
        let mut stats = ToolUsageStats::new();
        stats.record("a", 1, true);
        stats.record("b", 2, false);
        stats.record("a", 3, true);
        assert_eq!(stats.total_calls(), 3);
    }

    #[test]
    fn test_stats_to_json() {
        let mut stats = ToolUsageStats::new();
        stats.record("calc", 100, true);
        let j = stats.to_json();
        assert!(j.is_object());
        assert_eq!(j["calc"]["call_count"], 1);
        assert_eq!(j["calc"]["success_count"], 1);
        assert_eq!(j["calc"]["total_duration_ms"], 100);
    }

    #[test]
    fn test_stats_default() {
        let stats = ToolUsageStats::default();
        assert_eq!(stats.total_calls(), 0);
    }
}